This time, it wasn’t about history and hope, but a tougher, tighter calculation Virginia voters made about who could lead the nation out of its doldrums. Defying history for a second straight presidential election, Virginians cemented their new status as swing voters and chose Barack Obama.

For months, at unprecedented cost, both sides incessantly told voters that Virginia would determine the presidency. In the end, Obama’s strategy, aimed at the places and groups that were most loyal to the president, prevailed over Mitt Romney’s appeal to try a different way to jump-start the economy.

Nearly complete returns Tuesday night showed Obama winning in the same mostly urban parts of Virginia that he had in 2008 but by smaller margins. Romney drew better than Sen. John McCain did four years ago in many rural and suburban areas but not by much, especially in key Richmond suburbs where his campaign had predicted strong showings.

Exit polls suggested that evangelical Christians, most of whom supported the Republican, made up a considerably smaller portion of the Virginia electorate Tuesday than they had in 2008. Blacks, who voted overwhelmingly for Obama, made up about the same portion of the electorate that they did four years ago.

No Republican had lost Virginia for nearly half a century until four years ago, but the Obama campaign, which maintained operations in the state from 2008 straight through his first term, was convinced that even if the president remained unpopular and the economy stayed stuck in neutral, he could win.

Across the Old Dominion, Obama’s field operation included more than 60 offices, hundreds of paid organizers and more than 20,000 volunteers, who, on Saturday alone, placed 560,000 phone calls and knocked on 580,000 doors.

The Romney camp, for its part, dramatically boosted its efforts at persuading voters and getting them to the polls. To the very end, the Republicans put as much focus on Virginia as on any other state with the possible exception of Ohio. Romney spent an inordinate amount of time in Virginia over the campaign’s final 36 hours, visiting Fairfax, Lynchburg and Newport News. (All told, national candidates made more than 90 stops in Virginia this fall.)

Obama’s Virginia strategy was a two-story structure based on solid support from the D.C. suburbs of Alexandria and Fairfax and Arlington counties; from blacks, Hispanics and women in major population centers such as Hampton Roads; and from young voters.

The campaign then aimed at five densely populated places — this is the first election in which more than half of Virginians live in urban areas — where it hoped to match its 2008 performance.

Loudoun and Prince William counties outside Washington, Henrico and Chesterfield counties outside Richmond, and Virginia Beach are no one’s idea of Democratic strongholds; they grew up as mostly white, conservative bedroom communities. But in 2008, more than a decade of demographic change became apparent, as Obama found enough votes among blacks, Hispanics, women and immigrants to offset white men who hewed to traditional Republican leanings.

“It’s all Romney signs where we live,” said Liz Cusick of Ashburn, in a part of Loudoun that was mostly farmland two decades ago. She refrained from putting Obama stickers on her car because her husband thought it might inflame some neighbors. But she was determined to support the president: “He’s done a really good job with what he’s been given. He’s the one who cares about the middle class and people like us.”

Obama voters four years ago tended to be more liberal on social issues and less antagonistic toward government than many of their neighbors, but the unknown this year was whether widespread unemployment, the collapsed housing market and a general sense of stagnation would turn many of those voters toward a challenger who portrayed himself as ever less, in his own words, “severely conservative.”

Bob Schonder, who lives in western Loudoun County, voted for George W. Bush twice but went for Obama this year because “he did what he had to do to keep the country afloat. What scares me about Romney was he’s a hawk, and within six months, we’d be back in another war.”

Obama’s state director, Lise Clavel, said the campaign registered nearly 140,000 new voters in Virginia this cycle — mostly from demographic groups that favor Democrats, including 66,000 new black voters and 35,000 Latinos. Most of the new voters were both women and younger than 35.

The campaign then focused on turning voters out — particularly those with spottier voting histories. They weren’t just urged to vote; they were called, visited and asked to recite where and when they’d vote and how they’d get there.

Obama’s tactical generals said there was a qualitative difference in the kinds of contact the two campaigns made: Obama focused on personal appeals from friends or neighbors, whereas Romney’s contact count included mailings and robo-calls, Obama officials said.

The Romney camp argued that it had a big advantage in enthusiasm, especially after the first debate. “Their voters are not as excited about a second term as ours are about making a change,” said Romney’s political director, Richard Beeson.

Obama officials vehemently denied any enthusiasm gap.

Romney hammered at the president’s handling of the economy. But Virginia’s unemployment rate remained well lower than the national average, thanks largely to the D.C. suburbs, home to the nation’s three richest counties. In many places where Romney expected to do best, middle-class families saw signs of improvement.

“It is getting better,” said Susan Creeklore, a stay-at-home mother in Herndon. “And Obama’s values are closer to mine. I believe in gay rights and the Dream Act, and I have a preexisting condition, so I appreciate what he did on health care.”

“I genuinely don’t think that was a great strategy for them, because most Americans are like, ‘Hey, it isn’t as bad as it was four years ago,’ ” said Mike Henry, campaign manager for Democrat Timothy M. Kaine, who won his U.S. Senate bid.

The president promoted his decisions to wind down the wars in Iraq and Afghanistan and his attention to benefits for veterans. Obama spent millions targeting women by tying Romney to unpopular GOP positions in Virginia, including the legislature’s move to mandate ultrasounds for women considering abortion.

Although the candidates portrayed a stark choice between wildly different world views, some voters saw Obama and Romney as not terribly far apart.

Robb Warren, who lives in Norfolk and works aboard the USS Wisconsin, the Navy-vessel-turned-museum where Romney introduced his running mate, looks for centrists and concluded that “Obama’s on the left, but he’s closer to the middle than Romney is. I see Romney as pretty extreme, even if he’s trying to convince people he’s not.”

In the end, many of those who made history with Obama in 2008 stayed with him. In Prince William County, where a split identity — booming exurb to the west, many low-income black and Latino residents to the east — led to Obama’s win last time, his campaign scoured every precinct to turn out their voters, even feeding the last voters queued up deep into Tuesday night.

Corinne Reilly contributed to this report.