JavaScript tutorials for Beginners – JavaScript if…else Statement

 

 

(JavaScript Tutorials for Beginners)

In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript if…else Statement.

 

JavaScript if…else Statement

In this tutorial, you will learn about the if…else statement to create decision making programs with the help of examples.

In programming, there may arise a situation where you have to run a block of code among more than one alternatives. For example, assigning grades AB or C based on marks obtained by a student.

In such situations, you can use the JavaScript if...else statement to create a program that can make decisions.


In JavaScript, there are three forms of the if...else statement.

  1. if statement
  2. if…else statement
  3. if…else if…else statement

JavaScript if Statement

The syntax of the if statement is:

if (condition) {
    // the body of if
}

The if statement evaluates the condition inside the parenthesis ().

  1. If the condition is evaluated to true, the code inside the body of if is executed.
  2. If the condition is evaluated to false, the code inside the body of if is skipped.

Note: The code inside { } is the body of the if statement.

Working of if statement in JavaScript
Working of the if Statement

Example 1: if Statement

// check if the number is positive

let number = prompt("Enter a number: ");

// check if number is greater than 0
if (number > 0) {
 // the body of the if statement
  console.log("The number is positive");
}

console.log("The if statement is easy");

Output 1

Enter a number: 2
The number is positive
The if statement is easy

Suppose the user entered 2. In this case, the condition number > 0 evaluates to true. And, the body of the if statement is executed.

Output 2

Enter a number: -1
The if statement is easy

Suppose the user entered -1. In this case, the condition number > 0 evaluates to false. Hence, the body of the if statement is skipped.

Since console.log("The if statement is easy"); is outside the body of the if statement, it is always executed.

Comparison and logical operators are used in conditions. So to learn more about comparison and logical operators, you can visit JavaScript Comparison and Logical Operators.


JavaScript if…else statement

An if statement can have an optional else clause. The syntax of the if...else statement is:

if (condition) {
    // block of code if condition is true
} else {
   // block of code if condition is false
}

The if..else statement evaluates the condition inside the parenthesis.

If the condition is evaluated to true,

  1. the code inside the body of if is executed
  2. the code inside the body of else is skipped from execution

If the condition is evaluated to false,

  1. the code inside the body of else is executed
  2. the code inside the body of if is skipped from execution
Working of if-else statement in JavaScript
Working of the if…else statement

Example 2 : if…else Statement

// check is the number is positive or negative/zero

let number = prompt("Enter a number: ");

// check if number is greater than 0
if (number > 0) {
  console.log("The number is positive");
}
// if number is not greater than 0
else {
  console.log("The number is either a negative number or 0");
}

console.log("The if...else statement is easy");

Output 1

Enter a number: 2
The number is positive
The if...else statement is easy

Suppose the user entered 2. In this case, the condition number > 0 evaluates to true. Hence, the body of the if statement is executed and the body of the else statement is skipped.

Output 2

Enter a number: -1
The number is either a negative number or 0
The if...else statement is easy

Suppose the user entered -1. In this case, the condition number > 0 evaluates to false. Hence, the body of the else statement is executed and the body of the if statement is skipped.

 


JavaScript if…else if statement

The if...else statement is used to execute a block of code among two alternatives. However, if you need to make a choice between more than two alternatives, if...else if...else can be used.

The syntax of the if...else if...else statement is:

if (condition1) {
    // code block 1
} else if (condition2){
    // code block 2
} else {
    // code block 3
}
  • If condition1 evaluates to true, the code block 1 is executed.
  • If condition1 evaluates to false, then condition2 is evaluated.
    • If the condition2 is true, the code block 2 is executed.
    • If the condition2 is false, the code block 3 is executed.
Working of if-else if-else statement in JavaScript
Working of the if…else if…else statement

Example 3: if…else if Statement

// check if the number if positive, negative or zero
let number = prompt("Enter a number: ");


// check if number is greater than 0
if (number > 0) {
    console.log("The number is positive");
}
// check if number is 0
else if (number == 0) {
  console.log("The number is 0");
}
// if number is neither greater than 0, nor zero
else {
    console.log("The number is negative");
}

console.log("The if...else if...else statement is easy");

Output

Enter a number: 0
The number is 0
The if...else if...else statement is easy

Suppose the user entered 0, then the first test condition number > 0 evaluates to false. Then, the second test condition number == 0 evaluates to true and its corresponding block is executed.


Nested if…else Statement

You can also use an if...else statement inside of an if...else statement. This is also known as nested if…else statement.

Example 4: Nested if…else Statement

// check if the number is positive, negative or zero
let number = prompt("Enter a number: ");

if (number >= 0) {
    if (number == 0) {
        console.log("You entered number 0");
    } else {
        console.log("You entered a positive number");
    }
} else {
    console.log("You entered a negative number");
}

Output

Enter a number: 0
You entered number 0

Suppose the user entered 0. In this case, the condition number > -1 evaluates to true. Then the second condition number == 0 is checked which evaluates to true and its corresponding body is executed.

Note: As you can see, nested if...else makes your logic complicated. If possible, you should always try to avoid nested if...else.

 


Body of if…else With Only One Statement

If the body of if...else has only one statement, you can omit { } in the program. For example, you can replace

let number = 2;
if (number > 0) {
    console.log("The number is positive.");
} else {
   console.log("The number is negative or zero.");
}

with

let number = 2;
if (number > 0)
    console.log("The number is positive.");
 else 
   console.log("The number is negative or zero.");

The output of both programs will be the same.

Output

The number is positive.

Note: Although it’s not necessary to use { } if the body of if...else has only one statement, using { } makes your code more readable.


More on Decision Making

In certain situations, a ternary operator can replace an if...else statement. To learn more, visit JavaScript Ternary Operator.

If you need to make a choice between more than one alternatives based on a given test condition, the switch statement can be used. To learn more, visit JavaScript switch.

 

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

 

 

JavaScript tutorials for Beginners – JavaScript Operators

JavaScript tutorials for Beginners – JavaScript Comments

Beginners tutorial with R – If…Else Statement