In JavaScript, conditional statements are used to perform different actions based on different conditions. They allow your program to make decisions and execute certain blocks of code depending on whether given conditions are true or false. This article will walk you through the different types of conditional statements in JavaScript.
1. if
Statement
The if
statement executes a block of code if a specified condition is true.
if (condition) {
// Code runs if the condition is true
}
Example:
let score = 85;
if (score > 80) {
console.log("Excellent score!");
}
2. if-else
Statement
The if-else
statement executes one block of code if a condition is true, and another block if it is false.
if (condition) {
// Code runs if the condition is true
} else {
// Code runs if the condition is false
}
Example:
let isRaining = true;
if (isRaining) {
console.log("Take an umbrella.");
} else {
console.log("Enjoy the sunshine.");
}
3. if-else if-else
Ladder
When you have multiple conditions to check, you can chain if-else if-else
statements.
if (condition1) {
// Code runs if condition1 is true
} else if (condition2) {
// Code runs if condition2 is true
} else {
// Code runs if none of the above conditions are true
}
Example:
let marks = 75;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 75) {
console.log("Grade B");
} else {
console.log("Grade C");
}
4. Ternary Operator
The ternary operator is a shorthand for the if-else
statement.
condition ? expressionIfTrue : expressionIfFalse;
Example:
let age = 20;
let canDrive = (age >= 18) ? "Yes" : "No";
console.log(canDrive); // Output: Yes
5. switch
Statement
The switch
statement is useful when you want to compare a single expression against multiple possible values.
switch (expression) {
case value1:
// Code runs if expression === value1
break;
case value2:
// Code runs if expression === value2
break;
default:
// Code runs if no case matches
}
Example:
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week.");
break;
case "Friday":
console.log("Almost weekend.");
break;
default:
console.log("Midweek day.");
}
6. Short-Circuit Evaluation
JavaScript also uses logical operators for quick conditional execution.
-
&&
(AND) runs the second statement if the first is true. -
||
(OR) runs the second statement if the first is false.
Examples:
true && console.log("This will run"); // Runs because the first condition is true
false || console.log("This will run"); // Runs because the first condition is false
7. Nullish Coalescing Operator (??
)
The nullish coalescing operator returns the right-hand side value when the left-hand side is null
or undefined
.
let username = null;
let defaultName = username ?? "Guest";
console.log(defaultName); // Output: Guest
Conclusion
Conditional statements are an essential part of any JavaScript program. Mastering if
, if-else
, switch
, ternary operators, and short-circuit evaluations will enable you to write flexible and powerful code.
If you are serious about learning JavaScript in a structured and beginner-friendly way, check out this comprehensive ebook:
Learn JavaScript using this ebook:
https://codewithdhanian.gumroad.com/l/jjpifd
Top comments (0)