Menu Close

JavaScript If Else statement

In this tutorial, you will learn about JavaScript if else statement, syntax, and create a decision-making program with the help of examples.

In the previous JavaScript tutorial, we have seen all about JavaScript for loops to execute a block of code a number of times.

In computer programming, There are many situations array where we have to execute a block of code on the basic of different-2 conditions. In such situations, you can use JavaScript if else statement that can make decisions.

In JavaScript, we have following conditional statements.

  • if statement
  • if…else statement
  • if..else if…else statement

JavaScript if statement

JavaScript if statement is used to execute a block of code when the specified condition is true.

Syntax

if ( condition )
{
	// block of code to be executed.
	
};

Explanation:

The if statement evaluate the condition specified inside parenthesis ().

  • If the condition evaluates is evaluated true, the code inside the body of if is executed.
  • If the condition evaluates false, the code inside the body of if is skipped.

Flowchart of JavaScript if statement

JavaScript if statement flowchart

Example: Check a number is greater than 0 or not.

// check a number if greater than 10 
num = 20;
if ( num > 10 )
{
	console.log(`${num} is greater than 10`);	
}

Output will be:- 20 is greater than 10

JavaScript if else statement

JavaScript if statement has an optional clause else, that s used to execute a block of code when the condition is false.

Syntax

if ( condition )
{
	// block of code to be executed.
}

else 
{
	// block of code to be executed
};

Explanation:

The if statement evaluate the condition specified inside parenthesis ().

  • If the condition evaluates true, the code inside the body of if is executed.
  • If the condition evaluates false, the code inside the body of else is executed.

Flowchart of JavaScript if..else statement

JavaScript if else statement flowchart

Example: Check the age of the voter.

// Check a voter can vote or not by their age.

age = prompt("Enter your exact age here:- ");
if ( age > 18 )
{
	console("You are eligible to vote");
}
else 
{
	console.log("You are not eligible to vote");
};

Output 1:-


Enter your exact age here:- 20
You are eligible to vote

Output 2:


Enter your exact age here:- 15
You are not eligible to vote

JavaScript if..else if statement

JavaScript if..else if statement is used to execute a block of code when the previous condition is false.

Syntax

if ( condition 1 )
{
	// block code 1
}

else if ( condition 2 )
{
	// block code 2

else 
{
	// block code 3
};

Explanation:

  • If condition 1 is evaluated true, block code 1 is executed.
  • If condition 1 is evaluated false, condition 2 is evaluated.
  • If condition 2 is evaluated true, code block 2 is executed.
  • If condition 2 is evaluated false, code block 3 is executed.

Flowchart of JavaScript if..else if statement

javascript if else if statement flowchart

Example: Check a entered number is positive, negative or not.


number = prompt("Enter any number:- ")
if ( number > 0 )
{
	console("The Number is positive.");
	
}
else if ( number == 0 )
{

	console.log("The Number is zero.");
}
else 
{
	console.log("The Number is negative.");
};

Output 1:


Enter any number:- 10
The Number is positive.

Output 2:


Enter any number:- 0
The Number is zero.

Output 3:


Enter any number:- -10
The Number is negative.

Nested if..else statement

You can also use if..else statement inside another if..else statement, This is known as nested if..else statement.

Example:- Check a entered number is positive, negative or zero.


number = prompt("Enter any number:- ")
if ( number >= 0 )
{
	if ( number == 0 )
	{
	 
		console("Number is zero");
		
	}
	
	else 
	
	{
		console("Number is positive..");
		
	}
}
else 
{
	console.log("Number is negative");
};

Conclusion

In this tutorial, you have learned all about JavaScript if-else statements and different types of conditional statements with the help of examples. Conditional statements are very useful to execute a different-2 block of code on the basis of different-2 conditions.

Here we have seen simple examples but when you will work with real life poject, then you will perfomr more complex operations using decision making.

if you like this article, please share and keep visiting for further JavaScript tutorials.

JavaScript For Loop
JavaScript switch statement

Related Posts