Skip to main content

Command Palette

Search for a command to run...

Conditional Flow In JavaScript

Know about if else if else statement and switch case and when to use which

Published
5 min read
Conditional Flow In JavaScript
D

I have joined and started learning Software development with ChaiCode Web dev Cohort 2026 and very excited😊

In JavaScript the code runs line by line. but in certain condition we have to make decision into our program to run certain portion of code. Then There comes the use of conditionals which helps us to choose between one or multiple conditions. In this way, we can control the flow of our program so it is called control flow.

In JavaScript , There is If , else if and else and switch statement used for making decision and control the clow of program .

Statement Vs Expression

statement is the block of code that we do not store in variable. It is individually block and It do not return value. It may contain expressions. for examples like if-else, for loop etc.

expressions means the unit of code that resolved to result . like 2+3, name + " " + location

If , Else If , Else

Flowchart of if else statement

This is classical syntax which is used for conditional in programming that is also in JavaScript. When the result of expression is true then if block execute and other than that else block execute .

if true then if block run and if we want to check more condition then else if run otherwise else run .

// if

let num = 7
if (num ===7){
    console.log("7 for a reson")
}
console.log("here is end") ;

// if-else 
// check positive and negative

num = -3; 
if(num > 0) {
    console.log("Number is positive") ;
} else {
    console.log("Number is negative") ;
}


// to check zero consitiin also 
// else if used 
num =0 ;
if(num > 0) {
    console.log("Number is positive") ;
}else if(num < 0) {
    console.log("Number is negative") ;
}else {
    console.log("Number is exactly zero") ;
}

If else and Switch Case

Both are used for conditional branching but they have different use cases and performance characteristics.

if-else :

it executes the code blocks on the basis of boolean conditionals. They are versatile and can handle complex conditions with operators .

let score = 78 ; 
// score is sequentially check \
if(score >=90) {
    console.log("Grade A") ;
}else if (score >=80) {
    console.log("Grade B") ;
}else if(score >=70) {
    console.log("Grade C") ;
}else if(score >=60) {
    console.log("Grade D")
}else {
    console.log("Fail")
}
    

Advantages :

  • It is versatile and can be used to handle complex logic conditions including operators.

  • It is easier to use with simple and small set of conditions.

Disadvantages :

  • It can be slower for large conditionals as it execute sequentially.

  • It will be hard to read and maintain for nested conditions.

Switch Case :

It is used to execute the blocks of code for multiple options based on the value for single expressions. like

let days = 6;
switch(days) {
    case 1 :
        console.log("Sunday") ;
        break;
    case 2 :
      console.log("Monday") ;
      break;  
    case 3 :
        console.log("Tuesday") ;
        brak ;
    case 4:
        console.log("Wednesday") ;
        break;
    case 5 :
        console.log("Thursday") ;
        break;
    case 6 :
        console.log("Friday") ;
        break;
    case 7 :
        console.log("Saturday") ;
        break;
    default :
        console.log("Invalid day") ;
        
}

///////////////////////////////////////////////////
let day ="friday"
switch (day) {
        // can handle multiple case if you want to execute same code 
        // for multiple cases
        // if you do not break 
        // if faLL to the next case until found break
        
    case "saturday":
    case "sunday" :
        console.log("Cohort class");
        console.log("Weekend") ;
        console.log("Learning and Coding Day") ;
        break;

    case "monday":
    case "tuesday":
     console.log("Assignment day") ;
        break;
    case "wednesday" :
        console.log("Blog Day") ;
        break;
    case "thursday":
        console.log("T-Class Day") ;
        console.log("Bonus session and How to use brain to remember anything") ;
        console.log("By Akash Sir @yntp") ;
        break;
    case "friday" :
        console.log("Peer Class Day") ;
        break;

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

Advantages:

  • It is fast for larger conditions as it use jump table.

  • It is easier to read and maintain multiple conditions.

Disadvantages :

  • It can only used for equality. It does not used for complex logical expressions.

  • We have to use break statement carefully to prevent unintended fall-through.

When to use which :

It is simple. If you need to handle complex logic building which includes large number of logical expressions then you can go with if else .

but if you need to choose one among the multiple options and it is to check equality then this is proper place to use switch case.

Most of time 99 .5% of time we go through if else if statement.

Truthy and Falsy Values

In JavaScript there is concept of truthy and falsy because we can give any thing as value in conditional . Truthy means the boolean value comes true on conversion of values . same for falsy it gives false . The falsy values are :

  • false

  • 0 , -0, 0n

  • "", '', ``

  • null

  • undefined

  • NaN

  • document.all

These are falsy values which return false on conditionals . Besides these all are truthy in JavaScript like [], {}, "0", true etc.

Summary

This is all about conditional control flow in JavaScript. It is used to handle logical expressions and used to execute code on the basics of decisions in programming, It is used to build complex logic and easy to handle block of code on the basis of our logic.

Deep dive into Javascript

Part 6 of 14

this series includes all about Javascript which I will learned in Chaicode Webdev cohort 2026.

Up next

Handling Date and Time in JavaScript

Understanding Date Object of JavaScript