The condition statements in JavaScript are used to complete behaviors based on different conditions.
Instance
-
Switch statement
-
How to compile a switch statement.
Javascript switch statement
If you want to execute one of several code blocks, you can use the switch statement:
Syntax:
switch(n){case 1:Execution Code Block 1break case 2:Execution Code Block 2break default:If n is neither 1 nor 2, execute this code}
Working Principle: (N) after the switch can be an expression or (and usually) a variable. Then, the value in the expression is compared with the number in case. If it matches a case, the subsequent code is executed.BreakTo prevent the code from being automatically executed to the next line.
Instance:
<script type="text/javascript">//You will receive a different greeting based//on what day it is. Note that Sunday=0,//Monday=1, Tuesday=2, etc.var d=new Date()theDay=d.getDay()switch (theDay) { case 5: document.write("Finally Friday") break case 6: document.write("Super Saturday") break case 0: document.write("Sleepy Sunday") break default: document.write("I'm looking forward to this weekend!")}</script>