HOLLA TECH - LEARN

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>”);

 

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:

Holla Tech - Learn JavaScript

NOTE!
The final else statement “ends” the else if statement and should be always written after the if and else if statements.

else if
 

The final else block will be executed when none of the conditions is true.

Let’s change the value of the course variable in our previous example.

var course = 3;
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>”);

 

Result: 

Holla Tech - Learn JavaScript

NOTE!
You can write as many else if statements as you need.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE JAVASCRIPT MAIN COURSE PAGE. 

JAVASCRIPT MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT