if
... else if
StatementThe if
... else if
works just like if
, but it groups a number of conditions together. The keyword if
initiates the group; else if
indicates that it belongs to the preceding if
or else if
. If there is no if
preceding one or more else if
conditions, the JavaScript interpreter will return an error.
If an expression evaluates to true in any of the conditional statements, the corresponding code statement will be executed and the conditional block exited. Any subsequent else if
statements are ignored.
<script type="text/javascript"> <!-- function checkOptions(selectedValue) { //alert(selectedValue); if (selectedValue == 1) { document.write("Checking option 1"); document.write("1 = 1\nFound match."); } else if (selectedValue == 2) { document.write("Checking option 2"); document.write("2 = 2\nFound match."); } else if (selectedValue == 3) { document.write("Checking option 3"); document.write("3 = 3\nFound match."); } document.close(); } //--> </script>