DEV Community

Madhan Raj
Madhan Raj

Posted on

Conditional Statements in Javascript

JavaScript conditional statements are used to make decisions in a program based on given conditions. They control the flow of execution by running different code blocks depending on whether a condition is true or false.

*Conditions are evaluated using comparison and logical operators.
*They help in building dynamic and interactive applications by responding to different inputs.
Enter fullscreen mode Exit fullscreen mode

*Types of Conditional Statements *

1. if Statement
The if statement checks a condition written inside parentheses. If the condition evaluates to true, the code inside {} is executed; otherwise, it is skipped.

*Executes code only when a specified condition is true.
*Useful for making simple decisions in a program.
Enter fullscreen mode Exit fullscreen mode

Syntax:

if (condition) {
  // code runs if condition is true
}
Enter fullscreen mode Exit fullscreen mode
let x = 20;

if (x % 2 === 0) {
    console.log("Even");
}

if (x % 2 !== 0) {
    console.log("Odd");
};
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. It ensures that exactly one of the two code blocks runs.
Enter fullscreen mode Exit fullscreen mode

*Used when there are two possible outcomes.
*The else block runs when the if condition is not satisfied.
Enter fullscreen mode Exit fullscreen mode
let age = 25;

if (age >= 18) {
    console.log("Adult")
} else {
    console.log("Not an Adult")
};
Enter fullscreen mode Exit fullscreen mode

Output
Adult

3. else if Statement
The else if statement is used to test multiple conditions in sequence. It executes the first block whose condition evaluates to true.

*Allows checking more than two conditions.
*Evaluated from top to bottom until a true condition is found.
Enter fullscreen mode Exit fullscreen mode
const x = 0;

if (x > 0) {
    console.log("Positive.");
} else if (x < 0) {
    console.log("Negative.");
} else {
    console.log("Zero.");
};
Enter fullscreen mode Exit fullscreen mode

Output
Zero

4. Using Switch Statement (JavaScript Switch Case)

The switch statement evaluates an expression and executes the matching case block based on its value. It provides a clean and readable way to handle multiple conditions for a single variable.

*Used when one variable needs to be compared against multiple fixed values.
*Improves readability compared to long if...else if chains.
Enter fullscreen mode Exit fullscreen mode
const marks = 85;

let Branch;

switch (true) {
    case marks >= 90:
        Branch = "Computer science engineering";
        break;
    case marks >= 80:
        Branch = "Mechanical engineering";
        break;
    case marks >= 70:
        Branch = "Chemical engineering";
        break;
    case marks >= 60:
        Branch = "Electronics and communication";
        break;
    case marks >= 50:
        Branch = "Civil engineering";
        break;
    default:
        Branch = "Bio technology";
        break;
}

console.log(`Student Branch name is : ${Branch}`);
Enter fullscreen mode Exit fullscreen mode

output
Student Branch name is : Mechanical engineering

5. Using Ternary Operator ( ?: )
The ternary operator is a compact shorthand for an if...else statement. It is called β€œternary” because it takes three operands:

*A condition to test.
*An expression to evaluate if the condition is true.
*An expression to evaluate if the condition is false.[TBD]
Enter fullscreen mode Exit fullscreen mode

Syntax
condition ? expressionIfTrue : expressionIfFalse

let age = 21;

const result =
    (age >= 18) ? "You are eligible to vote."
        : "You are not eligible to vote.";

console.log(result);
Enter fullscreen mode Exit fullscreen mode

output
You are eligible to vote.

SUMMARY

Reasons to Use Conditional Statements

Control Program Flow: Decide which code to execute based on different situations.

Make Decisions: React differently to user input, data values, or system states.

Enhance Interactivity: Enable dynamic behavior in apps and websites.

Handle Multiple Scenarios: Manage different outcomes or error handling paths.

Improve Code Flexibility: Write adaptable, reusable code that can respond to change.
Enter fullscreen mode Exit fullscreen mode

Reference
https://www.geeksforgeeks.org/javascript/conditional-statements-in-javascript/

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

The conditional (ternary) operator is not a shorthand for the if statement. It's an operator, not a statement. It forms part of an expression, and is not a control flow statement.

It will evaluate one of two operands based on the truthiness of the first operand. It cannot run other statements or blocks of statements like if and other control flow statements can.

It's similar, but certainly not shorthand for an if/else.