Intro to JavaScript | Variables | Operators | Functions | If/else | For loops | Events

If/else statement

The if or if/else statement is used to perform a number of scripts if the statement is true/false.

Syntax
if (condition) {
// do something if the condition returns true
}
Example 1
var a=2;
var b=8;
if (a<b) {
alert("value a is smaller than value b");
} else if (a>b) {
alert("value a is larger than value b");
} else {
alert("value a and value b are equal");
}

Example 2
var foo="bar";
if (foo.length!=0) {
alert(foo);
} else {
alert("The length of the variable foo is zero");
}