Changes

Jump to: navigation, search

JavaScript Flow Control and Looping

922 bytes added, 19:16, 23 April 2007
The JavaScript ''if ... else'' Structure
=== The JavaScript ''if ... else'' Structure ===
 
The ''if'' statement above allows you to specify what should happen is 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 statement can be used to specify alternate action:
 
<pre>
if (customerName = "Fred")
{
document.write ("Hello Fred!");
}
else
{
document.write ("You're not Fred! Where's Fred?");
}
</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 match the string "Fred".

Navigation menu