Understanding JavaScript Conditional Statements

Understanding JavaScript Conditional Statements

ยท

4 min read

Meaning

Conditional statements allow you to execute specific blocks of code based on conditions. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.

Simple If

It executes a block of code if a condition is satisfied and will do nothing if the same results in false.

let a = 20
if (a < 100){
    a += 20
}
/* here, if the value of A is less than 100 only then it will enter in
the block otherwise will do nothing */

if....else

It executes if block if the condition is true and executes else if the condition results in false. Only will execute for a condition.

  • Use if-else when you need a default action if the condition is false.

  • An if-else statement includes an else block that runs if the if condition is false

let a = 100
if (a < 20){
    console.log("Smaller")
}
else{
    console.log("bigger") // result
}

else.....if.....else ladder

  • The if-else ladder evaluates conditions from top to bottom. Once a condition is true, the corresponding block of code executes, and no further conditions are checked.

  • The else if statement allows for multiple conditions, providing a clear structure to handle complex decision-making.

  • The syntax is simple just add another ifelse after else of previous flow

let a = 20
if (a < 10){
    console.log("10");
} else if (a < 20){
    console.log("20");
} else if (a < 30){
    console.log("30"); // result
}

Nested ifelse

  • It is used to check more complex conditions

  • we add another if inside if condition.

  • This approach allows you to create multiple layers of conditions, where the execution path depends on a combination of different criteria.

  • Every if must have its corresponding else

let a = 120
if (a < 100){
    if (a > 200){
        console.log(a)
    } else{
        console.log("no") // result
    }
} else {
    console.log("exit")
}

/* It will first check whether A is smaller than 100 or not. If yes do
check if the same is greater than 200 or not. If yes, do print the value
of A. If not, print "no" but if the first condition return false than do 
print "exit" */

Switch case

  • Use switch when you have a single expression with multiple possible outcomes.

  • Ensure break is used to prevent unintended fallthrough unless it's intentional.

  • Keep the default case to handle unexpected values or provide a fallback.

  • Avoid using switch for complex conditions or ranges; if-else or other control structures are better suited for such cases.

  • the switch statement provides an alternative to multiple if-else conditions when you need to evaluate a single expression against several possible outcomes.

  • A basic switch statement uses the switch keyword, followed by an expression to evaluate, and then one or more case statements to handle specific outcomes. The default keyword defines a block to execute if none of the cases match.

  • Break
    Each case typically ends with break to exit the switch block. Without break, execution "falls through" to the next case, which might be intentional in some scenarios.
  • default
    The default case is like an else block, handling any situation not covered by the defined cases
let day = "Monday";

switch (day) {
    case "Monday":
        console.log("Start of the workweek.");
        break;

    case "Wednesday":
        console.log("Midweek.");
        break;

    case "Friday":
        console.log("End of the workweek.");
        break;

    case "Saturday":
    case "Sunday":
        console.log("Weekend.");
        break;

    default:
        console.log("Unknown day.");
}

/* if day == "Monday" print first statement and break the flow 
of checking conditions and the same will check if first found
no match. In case "saturday" and "sunday" if result in any of them
then print "weekend" means they share same result */

You have Learned JavaScript conditional statements.................


If you want more such content then make sure to following me on all available platform

Instagram

LinkedIn

Hashnode


Thank you for reading ๐Ÿค

Written By Mohit Soni

ย