Switch statements
Learn how to execute selective instructions using switch statement.
We'll cover the following...
There is an abbreviated version for a long chain of if-else statement: the switch
operator. Switch is like a telephone operator. It puts you through some code in case the correct value is stored in your variable. There is also a default line, saying “the number you have dialed is invalid”.
Press + to interact
function logLampColor( state ) {switch( state ) {case 1:console.log( 'Red' );break;case 2:console.log( 'Yellow' );break;case 3:console.log( 'Green' );break;default:console.log( 'Wrong lamp state' );}}logLampColor( 1 );
Try this code ...