---
title: "Implementing if --- else in Java Script"  
description: "We can use “if—else “statements to perform an appropriate action based on condition. We can use” if—else” statement to take decision on the basis of s"  
author: "Anonymous User"  
published: 2011-03-08  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/495/implementing-if-else-in-java-script  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# Implementing if --- else in Java Script

We can use “if—else “statements to perform an appropriate action based on condition. We can use” if—else” statement to take decision on the basis of specific condition. If statements are executed if and only if condition passed to the if statements are evaluates to true otherwise else statements are executed.\

##### Syntax of if—else statement

```
If (<Boolean expression>) {                Statement1;                statement2;}
```

##### Following example demonstrate use of if---else statement

##### Example 1: if without else

```
function conditionalOperatorDemo() {            document.write("Following example demonstrate use of if block without else. <br />");            var res = 45;            if (res > 0) {                document.write("Number is positive. Condition evaluates to true. <br />");            }}
```

##### Output of the following code snippet is as follows

![if --- else in Java Script](https://www.mindstick.com/mindstickarticle/6d5d275f-9914-4e70-8e7a-6668d6ad5ba4/images/a10ffc94-4df9-4b1a-9747-0dc914c6cfb3.png)

##### Example 2: if with else statement

```
function conditionalOperatorDemo() {            document.write("Following example demonstrate use of if block with else. <br />");            var res = -55;            if (res > 0) {                document.write("Number is positive. Condition evaluates to true. <br />");            }            else {                document.write("Number is negative. Condition evaluates to false. <br />");            }}
```

##### Output of the following code snippet is as follows

![if --- else in Java Script](https://www.mindstick.com/mindstickarticle/6d5d275f-9914-4e70-8e7a-6668d6ad5ba4/images/90244ab8-12c4-461a-8442-89293d3d6525.png)

##### Example 3: Nested if--- else statement

```
function conditionalOperatorDemo() {document.write("Following example demonstrate use of nested if else block. <br />");            var age = 45;            if (age > 0) {                if (age > 20 && age < 50) {                    document.write("Your are valid for this post.");                }                else {                    document.write("You are overage for this post.");                }            }            else {                document.write("Invalid age you have entered.");            }}
```

##### Output of the following code snippet is as follows

![if --- else in Java Script](https://www.mindstick.com/mindstickarticle/6d5d275f-9914-4e70-8e7a-6668d6ad5ba4/images/68cff763-6634-4669-8e49-87142a9f64b9.png)

\

---

Original Source: https://www.mindstick.com/articles/495/implementing-if-else-in-java-script

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
