A loop in JavaScript is used to execute the same piece of code for a specified number of times (or when the specified condition is true).
JavaScript Switch Declaration
If you want to choose to execute one of several blocks of code, you can use the switch declaration:
Grammar:
Copy Code code as follows:
Switch (n)
{
Case 1:
Execute code block 1
Break
Case 2:
Execute code block 2
Break
Default
If n is not 1 or 2, then this code is executed
}
How it works: The (n) behind the switch can be an expression, or it can (and usually) be a variable. The values in the expression are then compared to the numbers in the case, and if they match one, the subsequent code is executed. The function of a break is to prevent the code from automatically executing to the next line.
Instance:
<script type= "Text/javascript" >//you'll receive a different greeting based//on 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") The Break Case 0:document.write ("Sleepy Sunday ") Break Default:document.write (" I ' m looking forward to this weekend! ")} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]