If you had a bad dream last night, it might have a positive benefit - because research suggests being scared while asleep helps to control fear during the waking hours.
University researchers in Switzerland and the United States examined how the brain responded to types of dream.
They found bad dreams improved the effectiveness of the brain in reacting to frightening experiences when awake.
But really terrifying nightmares were found to have a negative impact.
The neuroscientists, from the University of Geneva, the University Hospitals of Geneva in Switzerland and the University of Wisconsin in the US, have suggested that dreams could be used as a form of therapy for anxiety disorders.
Dreams 'prepare for real-life danger'
The study looked at whether bad dreams - which are moderately frightening rather than excessively traumatic - might serve a useful purpose.
With more than 250 electrodes attached to 18 subjects - and with another 89 people keeping diaries of their sleeping and dreaming - the researchers examined how the emotions experienced during dreams were connected with feelings when awake.
The findings, published in Human Brain Mapping, showed that bad dreams helped people to "react better to frightening situations".
When someone woke after a bad dream, the area of the brain that controlled their response to fear was found to be more effective.
This suggested that bad dreams were a way of preparing people for fear in their waking lives.
The greater the frequency of frightening dreams, the researchers found a higher level of activity in the area of the brain that manages fear.
"We were particularly interested in fear. What areas of our brain are activated when we're having bad dreams?" said Lampros Perogamvros, a researcher in the Sleep and Cognition Laboratory at the University of Geneva.
The researchers said they found a "very strong link between the emotions we feel in both sleep and wakefulness", with bad dreams being a way of simulating frightening situations as a rehearsal for such experiences when awake.
"Dreams may be considered as a real training for our future reactions and may potentially prepare us to face real life dangers," said Mr Perogamvros.
But there was a limit to how frightening a dream could be - because once a dream became a very upsetting nightmare the benefits were lost and instead it was likely to mean disrupted sleep and a "negative impact" that continued after waking.
"If a certain threshold of fear is exceeded in a dream, it loses its beneficial role as an emotional regulator," said Mr Perogamvros.