DEV Community

Code WithDhanian
Code WithDhanian

Posted on

JavaScript Conditional Statements

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
}
Enter fullscreen mode Exit fullscreen mode

Example:

let score = 85;

if (score > 80) {
  console.log("Excellent score!");
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Example:

let isRaining = true;

if (isRaining) {
  console.log("Take an umbrella.");
} else {
  console.log("Enjoy the sunshine.");
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Example:

let marks = 75;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else {
  console.log("Grade C");
}
Enter fullscreen mode Exit fullscreen mode

4. Ternary Operator

The ternary operator is a shorthand for the if-else statement.

condition ? expressionIfTrue : expressionIfFalse;
Enter fullscreen mode Exit fullscreen mode

Example:

let age = 20;
let canDrive = (age >= 18) ? "Yes" : "No";
console.log(canDrive); // Output: Yes
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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.");
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)