Skip to content

Instantly share code, notes, and snippets.

@jsonnull
Last active December 26, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsonnull/7137077 to your computer and use it in GitHub Desktop.
Save jsonnull/7137077 to your computer and use it in GitHub Desktop.
Just a simple hack I came up with to facilitate really short pattern-matching. It is effectively a chain of if-elseif statements. The first time a condition is true, the associated expression or code block is evaluated, and nothing further.
var x = false,
y = true;
// Call functions in the evaluation
(x && y)? alert("x and y matched")
: x? alert("x matched")
: y? alert("y matched")
: alert("default")
// Just do an assignment
var output =
(x && y)? "x and y"
: x? "x"
: y? "y"
: "none"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment