America under Jimmy Carter was unhappy and in decline, its economy crippled with stagflation, its diplomats held hostage in Tehran, and its befuddled president clueless before a seemingly unstoppable Soviet empire. “Malaise” became a catchword, and to Ronald Reagan’s simple but withering query — “Are you better off now than you were four years ago?” — Americans overwhelmingly answered: No.

But the gloom was far worse in Britain, a fiscal and moral wreck that was being called the “sick man of Europe.” Suffocating from Labor Party socialism, paralyzed by public-sector strikes, staggering under high taxes and double-digit inflation, Britain was visibly decaying at home and increasingly irrelevant abroad. That was the demoralized, debilitated nation that turned to Margaret Thatcher in 1979, sending her to 10 Downing Street for what would turn out to be the longest tenure of any British prime minister of the 20th century.

Of Thatcher’s self-confidence, ambition, and strong views, there could never have been much doubt. But who would have predicted that she would not only pull her country back from the brink, but restore it to greatness? Who foresaw that this grocer’s daughter from Grantham would become, with Winston Churchill, one of the two towering British statesmen of the 20th century?

“I am not a consensus politician,” Thatcher liked to say. “I am a conviction politician.” Those convictions were clear, and they dramatically transformed British history. Thatcher believed in capitalism and freedom, in rewarding risk-takers and encouraging entrepreneurs, in low taxes and private ownership. She loved her country, she cherished Anglo-American civilization, and she despised appeasement. She had a visceral abhorrence of communism, and rejected the accommodationists who saw Soviet ascendancy — and the West’s slow decline — as a permanent fact of life.

“The essence of Thatcherism was to oppose the status quo and bet on freedom,” The Economist noted in its obituary this week. “She thought nations could become great only if individuals were set free. Her struggles had a theme: the right of individuals to run their own lives, as free as possible from the micromanagement of the state.” That was the essence of Reaganism, too.

Decades later, it may seem obvious that Reagan and Thatcher were right — that the Cold War could be won, that liberal democracy wasn’t a lost cause, and that market freedoms were essential to national prosperity. But it wasn’t considered obvious then.

In 1983 the French philosopher Jean-Francois Revel published “How Democracies Perish,’’ an influential book that argued glumly that the free world lacked the spine and stamina to prevail in the long run against its enemies. “Perhaps in history democracy will have been an accident, a brief parenthesis which comes to a close before our very eyes,” Revel wrote. “Communism is a better machine for world conquest than democracy, and this is what will decide the final outcome of the struggle.”

Thatcher and Reagan remind us that things can change for the better, and great leaders can change them.

Thatcher and Reagan spurned such views, and they were mocked by their opponents as warmongers, dopes, and worse. One Labor MP called Thatcher “a half-mad old bag lady”; Reagan was notoriously derided as an “amiable dunce.” In 1985 Oxford University refused to grant Thatcher an honorary degree, the first time in decades that an Oxford-educated prime minister had been so snubbed.

Other leaders might have been undone by such hostility. It took uncommon fortitude for Thatcher to reverse the entrenchment of British socialism, and considerable nerve to go to war when Argentina seized the Falkland Islands. She and Reagan had no guarantees that the West would prevail in the Cold War. What they did have was the moral clarity to understand that it could — and that if they didn’t go wobbly, if they didn’t flinch from calling evil by its name, they might change the world for the better.

Defeatists and pessimists are always among us. It can be perversely tempting at times to imagine that our problems are too ingrained to fix, that the erosion is too far gone to reverse, that our enemies are too strong to defeat. But history is not predetermined. “Malaise” can give way to “morning in America.” The “sick man of Europe” can be restored to health.

Besides everything else they accomplished, Thatcher and Reagan remind us that things can change for the better, and great leaders can change them. It wasn’t foreordained that Britain and America would revive from the despondency of the 1970s. But voters in both countries elected leaders of conviction, not consensus. That made an extraordinary difference, and achieved a world of good.