---
title: "JavaScript – if…else Statement"
url: https://khanwaseem.com/blog/course/javascript-if-else-statement
author: "khan Waseem"
published: 2023-09-26
updated: 2023-09-26
topics: ["course", "Javascript Tutorial"]
description: "Learn about JavaScript’s if, else, and else if statements: fundamental tools for making decisions in your code and creating dynamic applications."
---

# JavaScript – if…else Statement

**Flow Chart Of If -Else** **: **

![](https://blogkhanwaseem.s3.amazonaws.com/uploads/2023/09/26130229/image-240.png)

Table of Contents

[Toggle](#)

- [if Statement :](#if-statement)
- [if else Statement :](#if-else-statement)
- [if else if Statement :](#if-else-if-statement)

## if Statement :

In JavaScript, the `if` statement is a fundamental construct that allows you to introduce decision-making and conditional logic into your code. It’s like giving your program the ability to make choices and take different actions based on certain conditions. Let’s dive into what the `if` statement does in plain human language:

- **Making a Decision**: Think of the `if` statement as a way to make a decision in your code. You set a condition that your program will check, just like you might decide whether to take an umbrella with you based on whether it’s raining outside.

- **Condition Evaluation**: The condition is like a question you ask your program. For example, you could ask, “Is it raining?” The program evaluates this condition and answers with either “yes” (true) or “no” (false).

- **Two Possible Paths**: Depending on the answer to your question (true or false), you provide two different sets of instructions for your program to follow.

- **Execution Based on Condition**: When your program encounters an `if` statement, it checks the condition. If the condition is true, it executes the instructions inside the `if` block. If it’s false, it skips those instructions and moves on.

Here’s a simplified example in everyday language:

```javascript
let isRaining = true;

if (isRaining) {
    // If it's raining, take an umbrella
    console.log("Don't forget your umbrella!");
}Code language: JavaScript (javascript)
```

In this scenario, you’re telling your program to check if it’s raining (the condition). If it is raining (true), your program prints a message about taking an umbrella.

The `if` statement is like a branching path in your code, allowing it to adapt and respond to different situations. It’s a fundamental tool in programming that enables your applications to behave intelligently and handle various scenarios with precision.

## if else Statement :

The `if...else` statement in JavaScript is a powerful tool for introducing decision-making capabilities into your code. It allows you to create branching logic, enabling your program to take different paths depending on whether a specified condition is true or false. Let’s break down the `if...else` statement in simple, human terms:

- **Conditional Decision-Making**: Think of the `if...else` statement as a way for your program to make decisions, just like you make choices in everyday life. It helps your code decide what to do next based on certain conditions.

- **Setting a Condition**: You begin by setting a condition or a question for your program. This condition is like asking your program something. For example, you might ask, “Is it daytime?” or “Is the user logged in?”

- **Two Possible Outcomes**: You provide two different sets of instructions, like two different paths your program can take. One path is for when the condition you set is true, and the other is for when it’s false.

- **Execution Based on Condition**: When your program encounters an `if...else` statement, it evaluates the condition. If the condition is true, it follows the instructions inside the `if` block. If it’s false, it goes down the `else` block.

Here’s a simplified example:

```javascript
let isDaytime = true;

if (isDaytime) {
    // If it's daytime (true), the program greets with "Good morning!"
    console.log("Good morning!");
} else {
    // If it's not daytime (false), the program says "Good night!"
    console.log("Good night!");
}Code language: JavaScript (javascript)
```

In this scenario, you’re telling your program to check whether it’s daytime (the condition). If it’s daytime (true), it greets with “Good morning!” If it’s not daytime (false), it says “Good night!”

Now, let’s add a bit more complexity:

```javascript
let userAge = 20;

if (userAge >= 18) {
    // If the user is 18 or older (true), the program says "You are an adult."
    console.log("You are an adult.");
} else {
    // If the user is younger than 18 (false), the program says "You are not an adult."
    console.log("You are not an adult.");
}Code language: JavaScript (javascript)
```

In this case, the program is making a decision based on the user’s age. If the user’s age is 18 or older (true), it considers them an adult. If they are younger than 18 (false), it says they are not an adult.

The `if...else` statement allows your code to adapt and respond intelligently to different scenarios, making it a crucial tool for building dynamic and interactive applications.

## if else if Statement :

Certainly! The `if...else if` statement in JavaScript is an extension of the basic `if` and `else` statements. It provides a way to introduce multiple conditions and execute different blocks of code based on the evaluation of those conditions. Let’s break down the `if...else if` statement in plain, human language:

- **Complex Decision-Making**: Think of the `if...else if` statement as a tool for making more complex decisions in your code. It’s like having a series of questions and answers, allowing your program to choose from multiple options.

- **Setting Multiple Conditions**: You start by setting multiple conditions, each representing a question. For example, you might ask, “Is it daytime?” and “Is the user logged in?”

- **Multiple Possible Outcomes**: For each condition, you provide a specific set of instructions, like different paths your program can take. One path is for when a particular condition is true, and the others are for when it’s false.

- **Sequential Evaluation**: When your program encounters an `if...else if` statement, it evaluates each condition sequentially from top to bottom. It stops at the first condition that is true and executes the corresponding instructions. If none of the conditions are true, it falls back to the `else` block, if provided.

Here’s a simplified example:

```javascript
let timeOfDay = "morning";

if (timeOfDay === "morning") {
    // If it's morning, the program says "Good morning!"
    console.log("Good morning!");
} else if (timeOfDay === "afternoon") {
    // If it's afternoon, it says "Good afternoon!"
    console.log("Good afternoon!");
} else if (timeOfDay === "evening") {
    // If it's evening, it says "Good evening!"
    console.log("Good evening!");
} else {
    // If none of the conditions match, it says "Hello!"
    console.log("Hello!");
}Code language: JavaScript (javascript)
```

In this scenario, you’re asking your program about the time of day (the conditions). Depending on the time, it responds with a specific greeting. If none of the conditions match, it defaults to saying “Hello!”

Now, let’s add a bit more complexity:

```javascript
let userAge = 20;

if (userAge < 13) {
    console.log("You are a child.");
} else if (userAge >= 13 && userAge < 20) {
    console.log("You are a teenager.");
} else if (userAge >= 20 && userAge < 65) {
    console.log("You are an adult.");
} else {
    console.log("You are a senior citizen.");
}Code language: JavaScript (javascript)
```

In this case, the program evaluates the user’s age (the conditions). If the age falls within a specific range, it categorizes the user accordingly, from child to senior citizen.

The `if...else if` statement provides a way for your code to handle more complex decision-making scenarios, making it a valuable tool when building applications with multiple possible outcomes and conditions to consider.

### Related posts:

[JavaScript – Switch Case](https://blog.khanwaseem.com/javascript-switch-case/)
[JavaScript – Operators](https://blog.khanwaseem.com/javascript-operators/)
[JavaScript – Loop Control](https://blog.khanwaseem.com/javascript-loop-control/)
[JavaScript – While Loop](https://blog.khanwaseem.com/javascript-while-loop/)

Powered by [YARPP](https://yarpp.com).
