The elements that make a joke hilarious include surprise, shock and the "kick of discovery," which is about having the listener of the joke think one way with a sense of false expectation, only to have it suddenly overturned. Other factors that make a joke successful include the framing, the telling and the punchline.
Scientists say that confusing the brain makes humans likely to laugh, a trait shared by other primates and a behavior regulated by the brain and associated with positive emotional stimuli. Because people have different thresholds to what leads to confusion, different things make different people laugh. The "kick of discovery" works the same way by leading the listener one way and suddenly shifting the listener's perception the other way. Shock and surprise support the "kick of the discovery," when the listener discovers that the joke's punchline wasn't what was expected.
Framing of a joke clues the listener in that a joke is about to be told and typical conversation markers to this effect include "Have you heard the..." and "Reminds me of this joke..." As jokes require a teller and listener, framing clues the teller whether the listener is receptive to listening to the rest of the joke. The telling follows framing and forms the body of the joke, with a narrative that builds anticipation in the listener and is about the joke's protagonist who becomes the butt of the joke. It needs to be succinct, containing only the bare details necessary to understand the punchline. The punchline triggers the humor by abruptly yanking the listener from his original and more obvious interpretation to a secondary, unexpected conclusion.