34,333
edits
Changes
→The JavaScript ''if ... else'' Statements
=== The JavaScript ''if ... else'' Statements ===
The ''if'' statement above allows you to specify what should happen is if a particular expression evaluates to ''true''. It does not, however, provide the option to specify something else that should happen in the event that the expression evaluates to be ''false''. This is where the ''if ... else'' construct comes into play.
The syntax for ''if ... else'' is the same as for the ''if'' statement with the exception that the esle ''else'' statement can be used to specify alternate action:
<pre>
</pre>
As shown in the above example the script following the ''if'' statement is executed when the expression is evaluated to be true (i.e the customerName variable contains the string "Fred") and the script after the ''else'' statement is executed when the customerName does nmot not match the string "Fred".
''if ... else'' structures can be taken a step further to implement ''if ... else ... if'' structures. For example: