Why do earthworms come up on sidewalks when it rains?

To mate

More information

They come to the surface as a response to high relative humidity after rain because they can move around safely without drying out. Some people believe this is an evolutionary response satisfying the need to disperse. But it is more simple than that.

Earthworms don't have lungs, they take in oxygen through their skin.

Underground they get oxygen from the air found between loose bits of soil. But when it rains, the air spaces fill up with water ( water can not hold as much oxygen as air ).

Earthworms can still get some oxygen from the water, But it soon runs out. So the earthworms crawl above ground. Above ground they get fresh air while they wait for the ground to dry. Some stay too long and dry out.

They can live up to two weeks under water as long as there is enough oxygen dissolved in the water

Oxygen dissolves into a mucous coating on a worm's skin then passes through the skin and into blood vessels. The skin must be moist for the oxygen to pass, that is why they wait until it rains.

There can be as many as a million earthworms under an acre of soil