Switch Statements

Estimated reading: 1 minute 57 views

Rather than using a series of if/else if/else blocks, sometimes it can be useful to use a switch statement instead. [Definition: Switch statements look at the value of a variable or expression, and run different blocks of code depending on the value.]

				
					 switch (foo) {
 case ’bar’:
 alert(’the value was bar-- yay!’);
 break;
 case ’baz’:
 alert(’boo baz :(’);
 break;
 default:
 alert(’everything else is just ok’);
 break;
 }
				
			
Share this Doc

Switch Statements

Or copy link