No one knows when the phrase "trick or treat" came into use, but the custom of kids walking neighborhoods to collect candy began in America in the 1920s. The Peanuts cartoon strip permanently embedded the phrase trick or treat into American Halloween pop culture in 1951. Disney further embedded trick-or-treat in Halloween tradition in 1952 with Donald Duck and his nephews Huey, Dewey and Louie starring in the cartoon feature "Trick or Treat."
Some of the first Halloween celebrations are attributed to the Celtic holiday of Samhain that took place in the Middle Ages. Participants dressed up as scary creatures in animal skins and went around to homes performing antics for food and drink.
When Christianity supplanted paganism around the ninth century, the holiday became official when November 2 was designated All Souls Day. In Scotland and Ireland, young people participated by wearing disguises. They would dress up and go around to houses performing tricks in order to get treats such as fruits, nuts and coins.
Large numbers of Irish immigrants that came to America during the potato famine of 1840 brought the tradition to America. The tradition expanded, and by the 1920s and 1930s excessive pranks and vandalism by rowdy young people had become a significant problem. This led to widespread community-based safe trick-or-treating, which was promptly halted with the start of sugar rations during World War II.