The if else if else Statement part 1

else if



You can use the else if statement to specify a new condition if the first condition is false.

Example:
var course = 1;
if (course == 1) {
document.write("<h1>HTML Tutorial</h1>");
} else if (course == 2) {
document.write("<h1>CSS Tutorial</h1>");
} else {
document.write("<h1>JavaScript Tutorial</h1>");
}
Try It Yourself

The above code says:
if course is equal to 1, output "HTML Tutorial";
else, if course is equal to 2, output "CSS Tutorial";
- if none of the above condition is true, then output "JavaScript Tutorial";

course is equal to 1, so we get the following result:
The final else statement "ends" the else if statement and should be always written after the if and else if statements
What keyword is used to end the "else if" statement?

end
stop
else

Comments

Popular posts from this blog

Comparison Operators part 2

Break and Continue part 1

Module 2