The title and lyrics of the song reference the popular motto and internet meme "YOLO" ("You Only Live Once"), a phrase popularized by Drake and Rick Ross on such songs as "The Motto", which were intended to promote a mixtape originally titled YOLO. The phrase has since become a cultural phenomenon and has been reworked to be an excuse for irresponsible and outlandish behaviour, but most of the time it is used sarcastically in this manner.
"YOLO" parodies the phrase, and once again reworks the phrase to mean something else. The song's lyrics interpret "You Only Live Once" to mean the complete opposite of dangerous and irresponsible behavior, and to instead be over secure and protective of life, in a pessimistic viewpoint. The song urges the public to be fully aware of their surroundings and the hazards of everyday life, since everything, in the writer's viewpoint, is a threat to life. The lyrics also satirize basic knowledge of economical events, dangerous activities and life-threatening events, and make reference to famous or not-so-famous acts done by other famous people or citizens ("Then bury all your money in the backyard like a Beagle", "Build a bomb shelter basement with titanium walls"). The song further parodies the acronym of YOLO at the end from "You Only Live Once" to You Oughta Look Out.
"YOLO" was written by all performers of the track, The Lonely Island, Adam Levine and Kendrick Lamar. Also credited for songwriting are Rhiannon Bryan and Rhydian Davies, members of the Welsh alternative rock band The Joy Formidable, whose 2011 single "Whirring" is sampled throughout the song. The song was produced by Needlz, who had previously worked with Lupe Fiasco and 50 Cent. The song was originally planned to feature Katy Perry, but two weeks before the song was released, this was changed to feature Maroon 5's Adam Levine, who was hosting Saturday Night Live the same night, and Kendrick Lamar, who had previously requested NBC to put him in a Saturday Night Live Digital Short or one of its many skits. The premiere of the song on SNL also marked the temporary return of Lonely Island frontman Andy Samberg, who had left Saturday Night Live the year before, but managed to get an appearance of The Lonely Island for the premiere of the song.