Wednesday, May 5, 2021

Alternative for a Switch statement

 

const something = 2;
// Longhand
switch (something) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
case 3:
doSomethingElseAndOver();
break;
}
// Shorthand
var cases = {
1: doSomething,
2: doSomethingElse,
3: doSomethingElseAndOver,
};
cases[2]();

No comments:

Post a Comment

function declaration, expression and call/invoke/execution

  The  function  declaration defines a function with the specified parameters. The  function   keyword can be used to define a function ins...