Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2021 20:25
What would you like to do?
How to IIFE the switch statement and more

First, credit to the answer at

Second, if you aren't comfortable with an IIFE (immediately executed function), you will want to check out

Now, because switch() {} is a "statement" in JavaScript and not a function, it can't be IIFE executed with the following

let failure = ( switch(x) { default: return true; } )(x);

You will get: "Uncaught SyntaxError: Unexpected token 'switch'"

This CAN be done using an "immediately-invoked lambda" (like IIFE, but lambda)

let success = ((x) => { switch(x) { default: return true; } })(x);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment