Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  If/Else Statements in JavaScript

If/Else Statements in JavaScript | JavaScript for Web Development - Software Development PDF Download

Introduction

When it comes to programming, decision-making is a fundamental concept. In JavaScript, one of the most commonly used tools for decision-making is the "if/else" statement. This article aims to provide a comprehensive guide to understanding and utilizing if/else statements in JavaScript. By the end of this article, you'll have a solid understanding of how to use if/else statements effectively in your code.

Understanding If/Else Statements

If/else statements allow you to execute different blocks of code based on a specified condition. They provide a way to make decisions in your program and control the flow of execution. The condition is evaluated, and if it is true, the code within the "if" block is executed. If the condition is false, the code within the "else" block is executed, if present.

Syntax of If/Else Statements

The syntax of a basic if/else statement in JavaScript is as follows:

if (condition) {

  // code to be executed if condition is true

} else {

  // code to be executed if condition is false

}

The "condition" in the above syntax can be any expression that evaluates to either true or false. If the condition is true, the code block within the "if" statement is executed. Otherwise, the code block within the "else" statement (if provided) is executed.

Examples and Code Explanations

Example 1: Basic If/Else Statement

Let's start with a simple example. Suppose we want to check if a number is positive or negative.


let number = 7;


if (number > 0) {

  console.log("The number is positive.");

} else {

  console.log("The number is negative.");

}

Output

The number is positive.

Code Explanation: In this example, the condition number > 0 is evaluated. Since the value of number is 7, which is greater than 0, the condition is true, and the code block within the if statement is executed. Therefore, "The number is positive." is printed to the console.

Example 2: Nested If/Else Statements

You can also nest if/else statements within each other to handle more complex conditions. Let's consider an example where we determine a student's grade based on their exam score.

let score = 85;

let grade = "";


if (score >= 90) {

  grade = "A";

} else {

  if (score >= 80) {

    grade = "B";

  } else {

    if (score >= 70) {

      grade = "C";

    } else {

      grade = "D";

    }

  }

}


console.log("Grade: " + grade);

Output

Grade: B

Code Explanation: In this example, the code checks the student's score and assigns the appropriate grade. If the score is greater than or equal to 90, the grade is set to "A". Otherwise, it checks the next condition using a nested if/else statement. The process continues until the appropriate grade is assigned based on the score. In this case, since the score is 85, the grade is "B".

Example 3: Multiple If/Else Statements

Multiple if/else statements can be used to handle different conditions independently. Let's consider an example where we categorize a person's age into different groups.

let age = 27;


if (age < 18) {

  console.log("You are a minor.");

} else if (age < 60) {

  console.log("You are an adult.");

} else {

  console.log("You are a senior citizen.");

}

Output

You are an adult.

Code Explanation: In this example, the code checks the value of the age variable using multiple if/else statements. If the age is less than 18, the person is categorized as a minor. If the age is less than 60, the person is categorized as an adult. If none of the previous conditions are met, the person is categorized as a senior citizen. Since the age is 27, the output is "You are an adult."

Sample Problems and Solutions

Problem 1: Write a JavaScript program that checks if a given number is even or odd.

let number = 10;


if (number % 2 === 0) {

  console.log("The number is even.");

} else {

  console.log("The number is odd.");

}

Output

The number is even.

Problem 2: Write a JavaScript program that determines if a given year is a leap year.

let year = 2024;


if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {

  console.log(year + " is a leap year.");

} else {

  console.log(year + " is not a leap year.");

}

Output

2024 is a leap year.

Conclusion

If/else statements are powerful tools for decision-making in JavaScript. They allow you to control the flow of your code based on specified conditions. By understanding the syntax and examples provided in this article, you now have a solid foundation to start using if/else statements in your JavaScript programs. Experiment with different conditions and build upon this knowledge to enhance your coding skills.

The document If/Else Statements in JavaScript | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Important questions

,

Exam

,

If/Else Statements in JavaScript | JavaScript for Web Development - Software Development

,

ppt

,

MCQs

,

Sample Paper

,

past year papers

,

Semester Notes

,

Objective type Questions

,

pdf

,

Free

,

study material

,

If/Else Statements in JavaScript | JavaScript for Web Development - Software Development

,

If/Else Statements in JavaScript | JavaScript for Web Development - Software Development

,

practice quizzes

,

video lectures

,

Extra Questions

,

Viva Questions

,

shortcuts and tricks

,

Summary

,

Previous Year Questions with Solutions

,

mock tests for examination

;