Conditional Statements

Javascript 8 min min read Updated: Mar 09, 2026 Beginner
Conditional Statements
Beginner Topic 7 of 15

Conditional Statements in JavaScript

In programming, a program often needs to make decisions based on certain conditions. Conditional statements in JavaScript allow developers to control the flow of execution by running different blocks of code depending on whether a condition is true or false.

For example, a website may need to check whether a user is logged in, whether a student has passed an exam, or whether a number is greater than another number. Conditional statements help programs perform actions based on such situations.

What are Conditional Statements?

Conditional statements are used to perform different actions depending on specific conditions. If the condition evaluates to true, a certain block of code is executed. If the condition evaluates to false, another block of code may be executed.

JavaScript provides several types of conditional statements:

  • if statement
  • if...else statement
  • if...else if...else statement
  • switch statement

1. The if Statement

The if statement is used to execute a block of code only if a specified condition is true.

javascript let age = 18; if (age >= 18) { console.log("You are eligible to vote."); }

If the condition age >= 18 is true, the message will be displayed in the console.

2. The if...else Statement

The if...else statement is used when there are two possible outcomes. If the condition is true, one block of code runs; otherwise, another block runs.

javascript let marks = 40; if (marks >= 50) { console.log("You passed the exam."); } else { console.log("You failed the exam."); }

In this example, if the marks are less than 50, the program prints "You failed the exam".

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

Sometimes a program needs to check multiple conditions. In such cases, the else if statement can be used.

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

The program checks conditions from top to bottom and executes the first matching condition.

4. The switch Statement

The switch statement is used when multiple conditions depend on the value of a single variable. It can be a cleaner alternative to multiple if...else statements.

javascript 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 stops the execution of the switch once a matching case is found.

Using Logical Operators in Conditions

Conditional statements often use logical operators such as && (AND), || (OR), and ! (NOT) to combine conditions.

javascript let age = 25; if (age > 18 && age < 60) { console.log("You are eligible for employment."); }

Here the condition is true only if both conditions are satisfied.

Why Conditional Statements are Important

Conditional statements allow programs to make decisions and behave differently based on situations. They are widely used in login systems, form validation, game logic, and application workflows.

Without conditional statements, programs would execute the same instructions every time without responding to different conditions.

Conclusion

Conditional statements are essential in JavaScript programming because they enable decision-making within a program. JavaScript provides several conditional structures such as if, if...else, else if, and switch.

By using these statements, developers can create dynamic programs that respond to different user inputs and conditions.

In the next tutorial, you will learn about JavaScript Loops, which allow programs to repeat tasks efficiently.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators