else Clause

By: Dusty Arlia
Published on October 23, 2012
Last Updated on Wednesday, July 08, 2015 at 8:38 PM
Total Updates: 5

When you are writing if statements, you have the option to include an else clause at the end. If you include an else clause at the end of your if statement, then you can refer to this conditional statement as an if/else statement.

The if/else statement has two paths - one that gets executed if the condition is true and one that gets executed if the condition is false. The else clause is the false path. The else clause is optional. If you were to leave it out of your if statement, then your program would continue to run and execute the next line of code.

Here is the basic syntax of the if/else statement:

if (/*condition*/){
//something executes in here if the condition is true
} else {
//something executes in here if the condition is false
}

And here is an example:

if (hungry == "yes") {
alert('Eat!');
} else {
alert('Sleep!');
}

Comments:

Ad: