(Patrik Svensson for The Washington Post)

Loneliness not only feels nasty, it can also make you depressed, shatter your sleep, even kill you. Yet scientists think loneliness evolved because it was good for us. It still is — sometimes.

There is plenty of evidence to suggest that being lonely ruins health. In one recent study, the risk of dying over a two-decade period was 50 percent higher for lonely men and 49 percent higher for lonely women than it was for those who did not experience feelings of isolation. According to some research, loneliness may be worse for longevity than obesity or air pollution.

Yet according to scientists such as John Cacioppo, a neuroscientist at the University of Chicago, loneliness has evolved to protect us. He likens it to hunger: “When you get hungry, it increases your attention to finding food. We think that loneliness is an aversive state that motivates you to attend to social connections.”

And just like pangs of hunger, loneliness can feel like real pain — at least inside the brain. When people who had been put in a functional MRI scanning device played a computer game that allowed them to be rejected by other players, the areas of the brain that lit up when they were rejected were the same ones associated with physical pain. The experiment, by UCLA psychologist Naomi Eisenberger and colleagues, proved that the anterior cingulate cortex, an area of the brain that becomes more active when we are in physical pain, also switches on when we experience the pain of social rejection.

That pain of loneliness, Cacioppo argues, could have motivated our ancestors to seek connection with other members of the tribe — and thereby improve their chances of survival and of passing on their genes.

In our genes?

Loneliness may push us to reconnect with others in ways that are often automatic and subconscious. In a 2015 experiment, volunteers were filmed as they watched clips from such movies as “Mrs. Doubtfire” and “American History X.” Those who previously had been made to feel socially rejected as part of the experiment were more likely than others to mimic the faces of the actors. If Robin Williams seemed happy, they’d make a happy face; if Edward Norton seemed sad, they’d look sad, too.

When we feel lonely, we may also try to get physically closer to others, which can be both physically and emotionally protective. A 2016 study showed that experiencing social pain makes people push their chairs closer to the person who had previously shunned them. It may appear trivial, but measuring the distance between chairs of people engaged in a conversation has long been used by psychologists to gauge the closeness of relationships.

Studies also suggest that how lonely you feel may have a genetic component; such findings imply that loneliness has evolutionary origins and may be heritable. A genetic component would explain why being friendless doesn’t cause the same misery for some people as it does for others. One gene responsible is the oxytocin receptor gene. Often called the love hormone, oxytocin has been found to boost maternal feelings, help people bond with others and increase trust.

“The oxytocin receptor gene comes in several variants, and there are indications that some of these variants make you respond in less reactive ways to oxytocin in your blood,” explains Luc Goosens, a developmental psychologist at the University of Leuven in Belgium. If you have the most common genotype of the oxytocin receptor gene, GG, you may be more attuned to the emotions of others but also more sensitive to rejection and more likely to end up feeling lonely.

In a 2015 study, undergraduates played a computer game in which they got sidelined by others: Only those who had the GG genotype ended up with elevated stress hormone, cortisol and higher blood pressure. Those with the AA genotype (about 15 percent of us have it) were less stirred by losing the game.

According to Goosens and Cacioppo, existence of these different genotypes makes sense from the evolutionary perspective. For our ancestors, safety was in numbers, so evolution favored those who craved the bond with others — and hence the GG genotype may have survived. On the other hand, the group also needed people less upset by being alone — those who might venture far away, explore the environment — such as the carriers of the AA genotype.

The downside

Even though loneliness may have been adaptive in the past, it can be detrimental in the 21st century. Lonely people, studies show, are more aggressive, more sleep-deprived and more likely to see unfamiliar people in a bad light, making it hard to operate in a society where we are surrounded all day by people we don’t know. “You push people away out of fear of being again threatened in social situations,” Cacioppo said.

A 2015 brain imaging study showed that loneliness makes our brains react differently to strangers and to people we know well: It activates “hunger” for reconnection when we see ones we know, but not when we face outsiders — and the lonelier someone is, the more this effect is seen. Such negativity toward people we don’t know made sense when we lived in small groups and “stranger danger” was particularly high, but it’s less good in a more atomized society when making new friends could help overcome solitude or isolation.

The way loneliness affects our bodies suggests how it may have been protective in the past but is no longer is. Studies show that sleep of lonely people tends to be fragmented and restless. Thousands of years ago, that behavior may have provided the hair-trigger readiness to deal with an approaching predator; today, it just makes you sleepy — and potentially less healthy — as you head to a 9-to-5 job. Cacioppo’s 2015 study found a change in immune cells among lonely people, suggesting why loneliness seems to make people more prone to catching colds.

Loneliness has also been found to elevate levels of the stress hormone cortisol.

In Paleolithic times, that would be good: Being on your own meant that you had to be constantly prepared to fight predators. Today, less so: “Short-term stress is a good thing. It helps us survive when we have a significant threat, but when we respond to [modern stressors] like traffic congestion and deadlines as if they were tigers, that’s a different thing,” Cacioppo said. The association between elevated stress and chronic diseases such as heart disease and obesity has been well documented.

The result, Cacioppo says, is that “when we remain lonely in a contemporary society for long periods, the costs start to outweigh the benefits.”

If you’re lonely, he says, it’s important to realize “that there isn’t something wrong with you, but rather that loneliness is a biological response that’s designed to help you and that part of the difficulty of getting out of the state in contemporary society is that it has these invisible effects, like all of a sudden finding people more threatening.”

In other words: It’s not you, it’s just your genes trying to help you survive the dangers of the Paleolithic wilderness.