---
title: "JavaScript – If Else"  
description: "Introduction: In this article I am going to explain about the if-else statement in JavaScript and how we can use it.When writing any program there are"  
author: "AVADHESH PATEL"  
published: 2012-11-19  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/394/javascript-if-else  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# JavaScript – If Else

##### Introduction:

In this article I am going to explain about the if-else statement in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) and how we can use it.

When writing any program there are may arise situations when we have to take [decisions](https://www.mindstick.com/articles/12962/4-important-decisions-to-make-when-booking-your-cruise) at many places. We have to [choose one](https://www.mindstick.com/forum/159291/what-are-some-popular-alternatives-to-mern-stacks-and-when-you-should-choose-one-over-the-other) path out of two so, like other [languages](https://yourviews.mindstick.com/story/1463/some-oldest-languages-in-the-world-still-spoken) JavaScript provides us conditional statement i.e. if-else statement so that we can take decision in [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) with JavaScript.

##### JavaScript supports three types of if-else-statements:

1. If-statement.

2. If-else statement.

3. If..else if...statement.

[If statement](https://www.mindstick.com/forum/157628/what-is-meant-by-if-statement-in-python):

If statement is the fundamental control statement it allows JavaScript to make decisions and execute statements only when certain [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) exists.

Syntax:

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

##### Example of If-else statement:

```
<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>If-else statement example</title>    <script type="text/javascript">        var age = 19;        if (age >= 18) {            document.write("Your are able to vote, Your age is :" + age);        }    </script></head><body></body></html>
```

##### If-else statement:

We will use [if else statement](https://www.mindstick.com/forum/55162/how-to-use-if-else-statement-in-java) to execute some code if certain condition is true and execute the other portion if condition is false. Here in either of the cases one block will execute.

##### Syntax:

```
if(condition)                                                                                                                                                                                      {  // code to be executed if condition is true.}else{//code that will be executed if condition is not true.}<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>If-else statement example</title>    <script type="text/javascript">        var age = 17;        if (age >= 18) {            document.write("Your are able to vote, Your age is :" + age);        }        else {            document.write("Your are not able to vote, Your age is :" + age);        }    </script></head><body></body></html>
```

##### If-else-if statement:

This is one step more [advanced](https://www.mindstick.com/articles/12993/5-advanced-features-for-your-odoo-ecommerce-theme) form of conditional statement that allows JavaScript to make a [correct decision](https://answers.mindstick.com/qa/51365/why-did-the-fdr-choose-to-curb-military-funding-during-the-new-deal-do-you-think-this-was-the-correct-decision-at-the-time) out of several choices.

##### Example of if-else-if statement

```
<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>If-else-if statement statement example</title>    <script type="text/javascript">        var percentage = 60;        if (percentage >= 75) {            document.write("Your are passed with honours :" + percentage);        }        else if (percentage >= 60) {            document.write("Your are passed with first division :" + percentage);        }        else if (percentage < 60 && percentage >= 35) {            document.write("Your are passed with second division :" + percentage);        }        else {            document.write("Fail :" + percentage);        }    </script></head><body></body></html>
```

##### Conclusions:

Through this article we came to know about the if-else statement and how we can use it in Java Script to perform condition checking.

\

---

Original Source: https://www.mindstick.com/blog/394/javascript-if-else

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
