Why did Jesus fast for 40 days and 40 nights?

Another answer from our community:

Jesus spent this period of fasting in order to prepare Himself for His earthly ministry which was about to begin. It also prepared Him for the time of temptation by the devil in the wilderness.

Another Answer
The number Forty is very special to the ancient Jews and early Christians; maybe for them the number 40 have some sort of Mystical Powers or Sympathetic Magic. There are maybe at least ten instances in the Old Testament and New Testament where 40 occurs, either in years or days. Example;. it rained for 40 days and 40 nights, Moses was on the mountain 40 days and 40 nights, the Israelites wandered 40 years, Jesus fasted in the wilderness for 40 days and was seen on the earth for 40 days after His crucifixion.
It is hard to explain why Jesus would need to fast for 40 days since, as the Son of God, he would have no spiritual need for such a challenge. However, the early Christians had not yet developed the more modern concept of Jesus as divine. In the earliest New Testament gospel, now known as the Gospel of Mark, Jesus was adopted by God as his son at Jesus' baptism. Thus, Jesus was still being seen as much more human than he is today.

Mark 1:13 is the original account of Jesus going into the wilderness for forty days immediately after his baptism by John. He was ministered by angels, an allusion to Elijah (1 Kings 19:5-7) who was ministered by an angel and in the wilderness forty days. There is no actual suggestion that Jesus fasted for this time, and being ministered by angels no reason for him to do so, but those familiar with the story of Elijah are likely to have assumed he did. This brings into play another allusion, to Moses when (Exodus 34:28) he fasted for 40 days while he wrote the words of the Ten Commandments on tablets. These allusions can be confirmed because this passage matches another reference to Elijah and Moses, in a literary sequence called a parallel structure.

Matthew and Luke do not follow Mark at this point, but follow the 'Q' document in which the allusion to Elijah differs in a subtle way, taking the angel out of the gospel story and instead having Jesus fast for forty days, just as Elijah did after the angel ministered to him. In this version, the further allusion to Exodus 34:28 is more readily recognised, and there is no longer any doubt that Jesus fasted.

The synoptic gospels tell us that Jesus fasted in the wilderness for forty days in order to draw the necessary parallel with Elijah and Moses.