MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript If / Else If / Else Control

In its most simple form, an if condition can be used like this:


var i = 0;
if (i < 1) {
 console.log("i is smaller than 1");
}

Output

"i is smaller than 1"

The condition i < 1 is evaluated, and if it evaluates to true the block that follows is executed. If it evaluates to false, the block is skipped.


An if condition can be expanded with an else block. The condition is checked once as above, and if it evaluates to false a secondary block will be executed (which would be skipped if the condition were true). An example:


if (i > 1) {
 console.log("i is smaller than 1");
} else {
 console.log("i was not smaller than 1");
}

Output

"i was not smaller than 1"

Supposing the else block contains nothing but another if block (with optionally an else block) like this:


var i = 1;
if (i < 1) {
 console.log("i is smaller than 1");
} else {
 if (i < 2) {
 console.log("i is smaller than 2");
 } else {
 console.log("none of the previous conditions was true");
 }
}

Output

"i is smaller than 2"

Then there is also a different way to write this which reduces nesting:


var i = 2;
if (i < 1) {
 console.log("i is smaller than 1");
} else if (i < 2) {
 console.log("i is smaller than 2");
} else {
 console.log("none of the previous conditions was true");
}

Output

"none of the previous conditions was true"

If any one condition evaluated to true, no other condition in that chain of blocks will be evaluated, and all corresponding blocks (including the else block) will not be executed.

The number of else if parts is practically unlimited. The last example above only contains one, but you can have as many as you like.

The condition inside an if statement can be anything that can be coerced to a boolean value, see the topic on boolean logic for more details;

The if-else-if ladder exits at the first success. That is, in the example above, if the value of i is 0.5 then the first branch is executed. If the conditions overlap, the first criteria occurring in the flow of execution is executed. The other condition, which could also be true is ignored.

If you want to execute multiple statements inside an if block, then the curly braces around them are mandatory.



Conclusion

In this page (written and validated by ) you learned about Javascript If Else-If Else Control . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Arrays.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.