According to a study, yawning occurs as a result of increase in the brain temperature. It is said to be a cooling mechanism for the brain. As per the study, both contagious and spontaneous yawn occurs due to this very reason. When we yawn after seeing someone else yawn, it is because we are sharing the same space and thus the temperature and environment our brain is exposed to is the same as them.
The temperature of our brain also fluctuates when we feel sleepy, stressed or bored and hence we yawn around this time.
Another study states that we yawn when the content of carbon dioxide in our blood increases. We yearn for oxygen at this time which can be provided by a yawn. However, it has also been stated that the amount of oxygen one inhales through yawning is less in comparison to normal respiration.
As per yet another theory, yawn occurs because of brain chemicals such as dopamine, serotonin, nitric oxide and glutamic acid. These are the same chemicals that impact our appetite, mood and other behaviour. An increase in these chemicals in the brain raises the frequency of yawning on the other hand the presence of endorphins in the brain decreases its frequency.