- switch
- case
- default
- break
Switch (case) in JavaScript
examples/js/switch.js
"use strict"; var n = 42; switch (n) { case 1: { console.log(1); break; }; case 42: { console.log(42); }; case 'other': { console.log('other'); }; default: { console.log('default') }; } // 42 // other // default
- You'd better call "break" at the end of each "case" statement or this will fall through and will execute all the lower cases without checking their condition.
- Switch uses === for comparision.
- We can put variables instead of the fixed values in the 'case' statements.
- If the same value appears in more than one 'case', only the first one will match.