VOOZH about

URL: https://dev.to/madhanraj/conditional-statements-in-javascript-4m92

⇱ Conditional Statements in Javascript - DEV Community


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.

*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.

Syntax:

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

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

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

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.

πŸ‘

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

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

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.
const x = 0;

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

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.
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}`);

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]

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);

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.

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