1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
const myVar = 4 const handleTwo = () => console.log('Two handler') const handleOdd = () => console.log('Odd handler') const handleDefault = () => console.log('Default handler') switch (true) { // NOTE THIS LINE! case myVar === 2: { handleTwo() break } case Boolean(myVar % 2): { handleOdd() break } default: { handleDefault() } } |
1 2 3 4 |
const myVar = 0 const result = {0 : 'case zero', 1 : 'case one'}[myVar] || 'default case' console.log(result) |