Conditional statements: Making decisions in JavaScript

Srijan } Author Pic
SrijanJun 08, 2025 - JavaScript
Conditional statements: Making decisions in JavaScript - Reacted Node

Photo: Conditional Statements - ReactedNode

What You Should Know

  • What JavaScript is and how to write basic statements.
  • How to run JavaScript in the browser.
  • What variables are and how to store values in them.

What You Will Learn

  • How to make decisions in your JavaScript code using conditional statements.
  • How do the if, else, and else if statements work?
  • What nested conditionals are and how to use them.

Key Concepts

What Is a Conditional Statement?

A conditional statement is a way to make decisions in a program. Based on whether certain conditions are true or false, your program can choose to execute specific blocks of code. Imagine your program reaches a point where it needs to decide which path to take - conditional statements provide the map.

The if statement

The if statement tells the program: "If this specific condition is true, then execute the code inside the following curly braces {} (code block)." If the condition is false, then skip the code block.

Syntax:

1if (condition) {
2 // Execute the code if the condition is true
3 // This is often called the if block
4}

Example:

1let age = 20;
2
3if (age >= 18) {
4 console.log("You are eligible to vote.");
5}
6// This line below will always execute, regardless of the if condition
7console.log("Program continues...");

The else statement

We use the else block to run code when the condition is not true. The else block always accompanies an if statement and executes its block of code when the if statement's condition is false.

Syntax:

1if (condition) {
2 // execute the code if the condition is true
3} else {
4 // execute the code if the condition is false
5 // This is the else block
6}

Example:

1let temperature = 15;
2
3if (temperature > 25) {
4 console.log("It's a hot day!");
5} else {
6 console.log("It's not a hot day."); // This will be printed
7}
8console.log("Weather check complete.");

The else if statement

We use the else if statement when we want to check for more than one condition. It allows us to chain multiple conditions together.

Syntax:

1if (condition1) {
2 // Execute the code if condition1 is true
3} else if (condition2) {
4 // Execute the code if condition2 is true
5} else {
6 // Execute the code if none of the above conditions are true
7}

Example:

1let score = 85;
2
3if (score >= 90) {
4 console.log("Grade: A");
5} else if (score >= 80) {
6 console.log("Grade: B"); // This will be printed
7} else {
8 console.log("Grade: C or lower");
9}

Nesting conditional statements

We can place conditional statements inside other conditional statements. This is called nesting. It allows for more intricate and layered decision-making logic.

1let age = 20;
2let hasID = true;
3
4if (age >= 18) {
5 if (hasID) {
6 console.log("You can enter."); // This will be printed
7 } else {
8 console.log("You need an ID.");
9 }
10} else {
11 console.log("You are too young to enter.");
12}

Why it matters

  • Conditional statements enable programs to think and make decisions.
  • Without conditional statements, programs would perform the same task every time, regardless of the circumstances.
  • Conditional statements are the foundation for creating interactive and dynamic behavior in websites, games, and apps.