The script has become familiar in this global season of far-right politics: A fringe politician peddling vitriol and promising order catches the mood of a nation yearning for change, any change, and rides it to the presidential palace.
A year ago, anyone who said Jair Bolsonaro could be elected president of Brazil would have been dismissed as a comic. A former artillery captain turned politician, Mr. Bolsonaro spent 27 years as an obscure congressman opposed to everything left-wing. In the campaign, he came to be best known for his outrageously offensive comments about gays, blacks, indigenous people and women and for defending the old military dictatorship, torture and guns.
His campaign platform, such as it was, was mostly about going backward -- pulling out of the Paris climate accord, using strong-arm tactics with criminals (his favorite motto is said to be, ''A good criminal is a dead criminal''), giving industry what it wants.
Yet his angry rants caught the mood of a Brazilian electorate sick of an endless corruption scandal that has reached to the far corners of the establishment, rampant street violence and economic dislocation, all of it indiscriminately and often unfairly blamed by many Brazilians on the left-wing Workers' Party, known as PT. The eagerness to repudiate anything PT -- and the political class as a whole -- overrode all other considerations, like Mr. Bolsonaro's total lack of preparation. He came in first in the first round and got a resounding 55 percent of the vote in the second.
Not surprisingly, President Trump, with whom Mr. Bolsonaro shares views on many issues ranging from gun rights to China, was among the first to proffer warm congratulations along with a cheery tweet (''Excellent call, wished him congrats!'').
Mr. Bolsonaro poses a danger to Brazil's democracy. Like Mr. Trump, he is a polarizing force -- he was seriously wounded by a would-be assassin during the campaign, and even before the election Brazilian media reported that police were staging raids in universities, purportedly to stop illegal electioneering. He is expected to name several former generals to his cabinet, a troubling move in a nation with a dark history of military control.
Yet in the immediate wake of the election, Mr. Bolsonaro pledged to respect democratic rules. ''This government will defend the constitution, democracy and liberty,'' he declared. ''This is a promise not of a party, not the empty words of a man; it's an oath before God.''
So far so good. And if he does manage to bring Brazil out of economic crisis, a task likely to be handed to the University of Chicago-trained economist Paulo Guedes, and to bring the crime rate and corruption under control without undermining the rule of law, so much the better. The initial reaction of Brazilian financial markets was a frenzy of stock-buying in the anticipation of policies like selling off inefficient state companies, deregulation and a cut in social spending.
The question is whether Brazil's still adolescent democratic institutions can withstand a far-right assault. Most of the measures Mr. Bolsonaro might attempt -- whether expanding the authority to carry arms or classifying the movement of landless people as ''terrorists'' -- would require either a law, which needs a simple majority in the legislature, or a constitutional amendment, which needs three-fifths. The new Congress is full of untried deputies, but, despite serious losses, the opposition Workers' Party is still the largest party in the lower house, with the potential to block Mr. Bolsonaro's more undemocratic initiatives.
Brazil's left is badly wounded, with the once-wildly popular former president, Luiz Inacio Lula da Silva, in prison. But the opposition would do best to recognize the election of Mr. Bolsonaro as a cry of desperation rather than a declaration of war, and to support those actions that address the wrongs while blocking those that endanger democracy.
Follow The New York Times Opinion section on Facebook , Twitter (@NYTopinion) and Instagram .
PHOTO: After 27 years as an obscure congressman, Jair Bolsonaro joined the club of reactionary populists rising to power. (PHOTOGRAPH BY ADRIANO MACHADO/REUTERS)