The "switch" statement
A switch statement could be used instead of many if conditions.
On the following example x is the value to be compared and case values are when x equals to that.
Using break will quit the switch statement. If it does not meet any conditions then it will be hitting default statement.
switch(x) {
case 'Approved': // if (x === 'value1')
alert("It has been already approved");
break;
case 'Pending': // if (x === 'value2')
alert("Please approve the item");
break;
default:
alert("This has not been submitted yet");
break;
}
- Comment Your JavaScript Code
- Declare Variables
- Data Types
- Type Conversions
- + Plus Operator
- - Minus Operator
- * Multiply Operator
- * Multiply Operator (1)
- % Modulus (Division Remainder) Operator
- ** Exponentiation Operator
- Bitwise operators
- Comparisons
- Logical Operators
- Interaction: alert, prompt, confirm, console.log
- The "switch" statement
- Loops: while, do while and for