Table of Contents
1. If statement
2. If…else statement
3. If…else if…statement
1. If statement
The if statement is the fundamental control statement that allows Node.js to make decisions and execute statements conditionally.
Syntax
The syntax for a basic if statement is as follows
if (expression) {
Statement(s) to be executed if expression is true;
}
Example:
var score = 6;
if (score > 5) {
console.log(“You’re passed!”);
}
Output:
You’re passed!
2. If…else statement
The if…else statement is the next form of control statement that allows Node.js to execute stataments in a more controlled way.
Syntax
if (expression) {
Statement(s) to be executed if expression is True;
} else {
Statement(s) to be executed if expression is False;
}
Example:
var score = 6;
if (score > 7) {
console.log(“You’re passed!”);
} else {
console.log(“You’re failed!”);
}
Output:
You’re failed!
3. If…else if… statement
The if…else if… statement is an advanced form of if..else that allows Node.js to make a correct decision out of several conditions.
Syntax
if (expression 1) {
Statement(s) to be executed if expression 1 is true;
} else if (expression 2) {
Statement(s) to be executed if expression 2 is true;
} else if (expression 3) {
Statement(s) to be executed if expression 3 is true;
} else {
Statement(s) to be executed if no expression is true;
}
Example:
if (score < 6) {
console.log(“Smaller”);
} else if (score == 6) {
console.log(“Equal”);
} else {
console.log(“Bigger”);
}
Output:
Equal
Thank you for reading!
You need to login in order to like this post: click here
YOU MIGHT ALSO LIKE