Musical episodes can be a risky change of pace, but when a television comedy finds the right combination of character, choreography and catchy songwriting, the results can linger long after the final number. Even dramas such as “Grey’s Anatomy” have joined the trend, though sitcoms are especially suited to turning emotional revelations and comic chaos into song.

These five episodes use the format in sharply different ways, from a demon forcing hidden truths into the open to a fever dream, an amateur stage production and a pointed “Glee” parody.

“Once More, with Feeling” — “Buffy the Vampire Slayer”

“Buffy the Vampire Slayer” may not be a traditional sitcom, but Joss Whedon’s blend of horror, romance, drama and humor made its Season 6 musical experiment a natural fit. In “Once More, with Feeling,” the demon Sweet leaves the Scooby Gang unable to keep their inner thoughts from spilling out in song.

Buffy fights vampires while performing “Overture/Going Through the Motions,” which addresses her depression following her death. The episode’s darker emotions are offset by playful theatrical flourishes, most notably in “I’ll Never Tell.” Anya and Xander sing directly to the audience as they wrestle with uncertainty in their relationship, dancing in a style associated with Fred Astaire and Ginger Rogers.

The result brings the show’s emotional intensity and campier instincts together without treating the music as a detached novelty.

“My Musical” — “Scrubs”

Fantasy sequences were already central to “Scrubs,” but Season 6’s “My Musical” shifts the perspective away from J.D. A patient with a temporal lobe aneurysm imagines the Sacred Heart Hospital staff singing throughout her stay.

Jeff Marx, known for “Avenue Q,” and Robert Lopez, who later wrote for “The Book of Mormon” and “Frozen,” created the episode’s music. Its numbers draw on classic Broadway styles: “Welcome to Sacred Heart” recalls “Another Opening, Another Show” from “Kiss Me Kate,” while Carla’s “For the Last Time, I’m Dominican” echoes the energy of “Tango: Maureen” from “Rent.” “Everything Comes Down to Poo” adds an absurd spin reminiscent of Irving Berlin’s “Puttin’ on the Ritz.”

The signature song is “Guy Love,” J.D. and Turk’s knowingly affectionate duet about their friendship. The revived “Scrubs” has been renewed for Season 11 and has not yet attempted another musical episode.

“The Nightman Cometh” — “It’s Always Sunny in Philadelphia”

Across 18 seasons, the “It’s Always Sunny in Philadelphia” gang has staged plenty of twisted schemes, including a children’s beauty pageant and an underground sweatshop. Yet the elaborate amateur production in Season 4’s “The Nightman Cometh” remains one of the series’ most memorable undertakings.

Charlie writes and stages an original musical in the hope of winning the Waitress’ heart. What follows involves a little boy, a princess, a cat-like figure called the Nightman and a troll demanding payment from those who want to see the boy. Much of the score is delivered through intentionally clumsy speak-singing, matching the show’s homemade theatrical setting.

The nonsensical declaration that Dayman is a “master of karate and friendship for everyone” captures the production’s appeal. Its upbeat presentation wraps around dark, uncomfortable material, while the gang’s lack of musical polish makes the spectacle feel entirely at home in the show’s world.

“Influenza: The Musical” — “Even Stevens”

“Influenza: The Musical” gives Ren a chance to move beyond her usual role as Louis’ strait-laced counterpart. During a fever, she imagines classmates, teachers, relatives and friends launching into elaborate musical sequences.

John Coda composed the music, Kim Blank handled the intricate choreography and John Wise wrote the lyrics. Wise later wrote songs for “Mad TV,” “The SpongeBob SquarePants Movie” and “That’s So Raven.” Christy Carlson Romano, who plays Ren, later portrayed Belle in a national tour of “Beauty and the Beast.”

The episode moves among several theatrical styles. Principal Wexler opens with the jazzy “Morning Announcements,” while “Master of the Gym” draws from “The Phantom of the Opera” and “Les Misérables.” “Sixth Period” evokes the beginning of “I Wanna Be a Producer” from “The Producers.”

Its most enduring number is Ren’s improvised school presentation, “We Went to the Moon in 1969.” The deliberately straightforward lyric — “We went to the moon in 1969, not 1968, but the year after” — is paired with a simple, repetitive melody designed for instant recall.

“Regional Holiday Music” — “Community”

“Community” regularly played with sitcom conventions, including a “Star Wars”-inspired paintball episode and a stop-motion Christmas special. Season 3’s “Regional Holiday Music,” which aired in 2011, turned that experimental streak toward a satire of Ryan Murphy’s “Glee.”

Mr. Rad, an excessively enthusiastic glee club teacher, recruits the study group for the annual Christmas pageant. The spoof extends to the room’s risers and its unexplained bearded piano player.

Donald Glover performs “We Have to Save Christmas to Save Our Friends” in a sequence styled like an early-2000s music video, complete with extreme close-ups and cool blue tones. Annie uses a babyish, Betty Boop-like voice for “Santa Seduction,” an attempt to entice Jeff while wearing a sexy Santa outfit.

“Baby Boomer Santa” makes the broadest musical sweep, shifting from boogie-woogie and rockabilly to disco. Pierce, played by Chevy Chase, joins the number with a gravelly delivery. By targeting a major television phenomenon of its moment, “Regional Holiday Music” turns the musical episode itself into both a celebration and a punchline.