Google "Donald Trump" and "nationalism" and you'll get 1,990,000 results, the large percentage of which are, to judge from the top hits, negative. "Nationalism" is deemed to be bad stuff, maybe even akin to Nazism.

But is nationalism always so bad? Not, it seems, for the millions of people around the world watching the Rio 2016 Olympics. They watch as the TV networks keep track of how many medals each nation's athletes has won — and they root for the men and women they see as representing their nation.

Americans thrilled to see Michael Phelps propel the U.S. team to gold in the freestyle relay and a 19-year-old Katie Ledecky race way ahead of the field in the 400 meter freestyle. People who watch gymnasts at only four-year intervals were amazed at the skill of the 4'9" Simone Biles.

News coverage in other countries focused on their own athletes. British front pages flashed pictures of record-breaking breast stroker Adam Peaty, mouthing the words of "God Save the Queen" as he held his gold medal. Brazil's TVGlobo showed judo medalist Rafaela Silva, who grew up in a Rio favela, bow down on her knees to Brazilian fans in the stands.

Sports nationalism easily embraces ethnic and racial diversity, not only from historically biracial America and Brazil (which abolished slavery in 1865 and 1888) but also from European and other nations. One Olympic table tennis match featured a Japanese-descended Brazilian and a Chinese-descended Congolese. People from nations with sharply divisive politics (not least our own) and suffering from economic setbacks and pervasive corruption (like the Olympics host Brazil) nonetheless find themselves united in rooting for their country's athletes.

An elite globalist may scoff at the arbitrariness of national borders and style himself "a citizen of the world," as President Obama described himself before a massive crowd in Berlin in 2008. But most people don't think of themselves that way. Nation-states inspire loyalties in a way the United Nations or the European Union have failed to do.

Nationalism, properly understood, can be a positive force, welding otherwise disparate people together to build a decent society, secure a competent government and rally to defend themselves against attack. Over the course of history each nation has developed its own particular culture, its own manners and mores, its own rules written and unspoken.

An intelligent nationalist can respect the strengths of other nationalisms, while preferring his own, just as an Olympics fan can appreciate the superb performance of athletes from other countries even while keeping an eye on the scoreboard showing the number of medals each country has won.

The social psychologist Jonathan Haidt, writing in the American Interest, notes that as nations grow more prosperous, their elites become more globalist in outlook, and consider nationalism as blind prejudice or even racism. But, as he writes, "having a shared sense of identity, norms and history" — i.e., nationalism — "generally promotes trust."

"Nationalists feel a bond with their own country, and they believe that this bond imposes moral obligations both ways," he goes on. "Citizens have a duty to love and serve their country, and governments are duty bound to protect their own people."

This is a principle that Donald Trump, in between off-the-cuff gaffes and self-harming diversions, affirms. Nations have boundaries and owe greater duties to their citizens than to foreigners. They have no obligation to open their borders entirely. It is not racist, Haidt argues, to bar those "whom they perceive as having values that are incompatible with their own, or who (they believe) engage in behaviors they find abhorrent, or whom they perceive to be a threat to something they hold dear."

Hillary Clinton takes a different view. She would not deport any non-criminal illegal immigrant, which amounts to a permanent open borders policy — as extreme a position as Trump's now discarded ban on Muslim immigration.

But even Democrats at their national convention found it useful to sound nationalist themes, decrying Trump's "dark" picture of America in his acceptance speech as somehow unpatriotic and, after conservative bloggers noted their absence on the convention's first day, installing American flags on the podium.

And former Treasury Secretary and Obama adviser Lawrence Summers has called for "a responsible nationalism" which recognizes government's responsibility "to maximize the welfare of citizens, not to pursue some abstract concept of the global good."

Evidently nationalism, like rooting for your nation's Olympians, is not necessarily a bad thing.