Skip to main content

Command Palette

Search for a command to run...

Conditional Statements in JavaScript

Updated
4 min readView as Markdown

Yesterday, I learned how JavaScript uses operators to perform calculations and compare values.

Today, I explored Conditional Statements, which allow a program to make decisions based on different conditions.

Just like humans make decisions every day, JavaScript also needs a way to choose what code should run depending on a situation.

Let's dive in.


What are Conditional Statements?

Conditional statements allow JavaScript to execute different blocks of code depending on whether a condition is true or false.

Example:

let age = 20;

if (age >= 18) {
    console.log("You can vote.");
}

Since the condition is true, the message is displayed.


Why Do We Need Conditional Statements?

Imagine building:

  • Login Systems

  • ATM Machines

  • Online Shopping Websites

  • Weather Apps

  • Games

All of them need to make decisions.

For example:

  • Is the user logged in?

  • Is the password correct?

  • Is the product in stock?

  • Is the score greater than 100?

Conditional statements solve these problems.


The if Statement

The simplest conditional statement.

let temperature = 32;

if (temperature > 30) {
    console.log("It's a hot day!");
}

If the condition is true, the code inside the block executes.


The if...else Statement

Runs one block if the condition is true and another if it's false.

let isLoggedIn = false;

if (isLoggedIn) {
    console.log("Welcome back!");
} else {
    console.log("Please log in.");
}

Only one block executes.


The if...else if...else Statement

Used when there are multiple possible conditions.

let marks = 82;

if (marks >= 90) {
    console.log("Grade A");
}
else if (marks >= 75) {
    console.log("Grade B");
}
else if (marks >= 50) {
    console.log("Grade C");
}
else {
    console.log("Fail");
}

JavaScript checks each condition from top to bottom.

The first true condition is executed.


Nested if Statements

An if statement can be placed inside another if.

let age = 22;
let hasLicense = true;

if (age >= 18) {

    if (hasLicense) {
        console.log("You can drive.");
    }

}

Nested conditions are useful when multiple requirements must be satisfied.


The switch Statement

Used when checking a single value against many possible cases.

Example:

let day = 3;

switch (day) {

    case 1:
        console.log("Monday");
        break;

    case 2:
        console.log("Tuesday");
        break;

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

    default:
        console.log("Invalid Day");

}

The break statement prevents execution from continuing into the next case.


if...else vs switch

if...else switch
Good for ranges Good for exact values
More flexible Cleaner for many fixed options
Complex conditions Single variable comparisons

Choose the one that best fits the problem.


Truthy & Falsy Values

JavaScript automatically treats some values as true or false.

Falsy values:

  • false

  • 0

  • "" (empty string)

  • null

  • undefined

  • NaN

Everything else is generally considered truthy.

Example:

let username = "";

if (username) {
    console.log("User Found");
}
else {
    console.log("No Username");
}

Output:

No Username

Best Practices

✔ Write simple and readable conditions.

✔ Avoid deeply nested if statements.

✔ Use switch when checking one variable against many fixed values.

✔ Use strict comparison (===) whenever possible.

✔ Keep your conditions easy to understand.


My Biggest Takeaway

Today, I learned that programming is all about making decisions.

Conditional statements allow JavaScript to respond differently depending on different situations.

Whether it's validating a login, checking grades, or controlling game logic, decision-making is at the heart of programming.

Understanding if, else, else if, and switch has given me the tools to write programs that can react intelligently instead of following a single fixed path.

2 views

100 Days of Code: My Journey to Becoming a Full Stack Developer

Part 34 of 50

Welcome to my 100 Days of Code journey! In this series, I'll document my daily progress as I learn Full Stack Web Development from the ground up. Every post will cover what I learned, challenges I faced, mistakes I made, and the projects I built. My goal is not just to complete 100 days but to become a better developer through consistency, discipline, and learning in public. Topics I'll cover include: • Git & GitHub • HTML, CSS & JavaScript • React.js • Node.js & Express • MongoDB • APIs • Real-world Projects • AI tools for Developers Whether you're just starting out or revising your fundamentals, I hope this journey helps you learn alongside me. Let's build, learn, and grow together! 🚀

Up next

Loops in JavaScript – Automating Repetitive Tasks

Yesterday, I learned how JavaScript makes decisions using Conditional Statements. Today, I explored Loops, one of the most useful concepts in programming. Loops allow us to execute the same block of c

More from this blog

T

TheSaurceCode

73 posts

Documenting my journey to becoming a Full Stack Developer through daily blogs, coding challenges, projects, tutorials, and lessons learned. Learn, build, and grow with me