Example 1: Using else if
let color="blue";
if(color === "red"){
console.log("stop!");
} else if (color === "yellow"){
console.log("Ready!");
} else if (color === "green"){
console.log("Go!");
} else {
console.log("unknown color");
}
Example 2: Nested if condition
let user ="admin";
let password ="1234";
if(user === "admin"){
if(password === "1234"){
console.log("login successful!");
} else {
console.log("Wrong password");
}
} else {
console.log("user not found");
Try Yourself
1.Write a program that checks if a number is positive,negative, or zero.
2. Check student marks:
If marks>=35
If marks>=90 --> "Excellent"
Else --> "pass"
Else --> "Fail"
Top comments (0)