|
| IF ELSE STATEMENTS IN JAVASCRIPT
An if else statement is used to test a certain condition. If it is true, certain specified code between the first curly braces will execute. If it is false, another specified code between the second set of curly braces will execute.
The general format of the if statement is:
if(certain condition)
{
execute this code;
}
else
{
execute another code;
}
Example:
var username="dolly24";
if(username=='dolly24')
{
alert("hello " + username);
}
else
{
alert("You are not welcome here!!");
}
With the above code we are saying that if the username is dolly24, must an alert message display with Hello dolly24 (code between the first curly braces).
Therefore, if the username is anything else besides dolly24, the code between the second set of curly braces must execute, in this case output: You are not welcome here!!
Hello dolly24 will display as we have declared the username to be dolly24 in the first line of the code. If we change the username to frank or anything else besides dolly24, then the code between the second curly braces executes which will display: You are not welcome here!!
 |
|
|