JavaScript if Statement

In this chapter, we will learn about the JavaScript if statement. This statement allows you to execute a block of code based on a condition. We will cover:

  • JavaScript if Statement
  • JavaScript else Statement
  • JavaScript else if Statement
  • Nested if Statements
  • Simple Programs using if Statements

JavaScript if Statement

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

Syntax

if (condition) { // code to be executed if the condition is true } 

Example

let age = 18; if (age >= 18) { console.log("You are an adult."); } // Output: You are an adult. 

In the example above, the condition age >= 18 is true, so the code inside the if block is executed.

JavaScript else Statement

The else statement is used to execute a block of code if the if condition is false.

Syntax

if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false } 

Example

let age = 16; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); } // Output: You are a minor. 

In the example above, the condition age >= 18 is false, so the code inside the else block is executed.

JavaScript else if Statement

The else if statement is used to specify a new condition to test if the first condition is false.

Syntax

if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition1 is false and condition2 is true } else { // code to be executed if condition1 and condition2 are false } 

Example

let marks = 75; if (marks >= 90) { console.log("Grade: A"); } else if (marks >= 80) { console.log("Grade: B"); } else if (marks >= 70) { console.log("Grade: C"); } else { console.log("Grade: D"); } // Output: Grade: C 

In the example above, the first condition marks >= 90 is false, so the code checks the next condition marks >= 80, which is also false. Then it checks the condition marks >= 70, which is true, so the code inside the else if block is executed.

Nested if Statements

Nested if statements are if statements inside another if statement. They are used to test multiple conditions.

Syntax

if (condition1) { // code to be executed if condition1 is true if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if condition2 is false } } else { // code to be executed if condition1 is false } 

Example

let age = 20; let hasID = true; if (age >= 18) { if (hasID) { console.log("You are allowed to enter."); } else { console.log("You need an ID to enter."); } } else { console.log("You are not allowed to enter."); } // Output: You are allowed to enter. 

In the example above, the outer if condition age >= 18 is true, so the code checks the nested if condition hasID. Since hasID is true, the code inside the nested if block is executed.

Simple Programs using if Statements

Program 1: Check if a Number is Positive, Negative, or Zero

let number = -10; if (number > 0) { console.log("The number is positive."); } else if (number < 0) { console.log("The number is negative."); } else { console.log("The number is zero."); } // Output: The number is negative. 

Program 2: Check if a Number is Even or Odd

let number = 7; if (number % 2 === 0) { console.log("The number is even."); } else { console.log("The number is odd."); } // Output: The number is odd. 

Program 3: Find the Largest of Three Numbers

let num1 = 10; let num2 = 20; let num3 = 15; if (num1 >= num2 && num1 >= num3) { console.log("The largest number is " + num1); } else if (num2 >= num1 && num2 >= num3) { console.log("The largest number is " + num2); } else { console.log("The largest number is " + num3); } // Output: The largest number is 20 

Conclusion

In this chapter, you learned about the JavaScript if statement, including if, else, else if, and nested if statements. We also covered some simple programs using if statements to demonstrate their usage. These control flow statements are essential for making decisions in your code based on different conditions. Understanding how to use them will help you write more complex and functional programs.

Leave a Comment

Scroll to Top