---
description: Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform anothe
title: Conditional Statements in JavaScript: if, else, and else if
image: https://www.guru99.com/images/how-to-use-conditional-statements-in-javascript.png
---

 

[Skip to content](#main) 

## JavaScript Conditional Statements

There are mainly three types of conditional statements in JavaScript.

1. **if Statement**: An ‘if’ statement executes code based on a condition.
2. **if…else Statement**: The if…else statement consists of two blocks of code; when a condition is true, it executes the first block of code and when the condition is false, it executes the second block of code.
3. **if…else if…else Statement**: When multiple conditions need to be tested and different blocks of code need to be executed based on which condition is true, the if…else if…else statement is used.

## How to use Conditional Statements

Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action.

[![Use Conditional Statements in JavaScript](https://www.guru99.com/images/JavaScript/javascript5_1.png)](https://www.guru99.com/images/JavaScript/javascript5%5F1.png)

## If statement

**Syntax:**

if(condition)
{
	lines of code to be executed if condition is true
}

You can use `if` statement if you want to check only a specific condition.

**Try this yourself:**

<html>
<head>
	<title>IF Statments!!!</title>
	<script type="text/javascript">
		var age = prompt("Please enter your age");
		if(age>=18)
		document.write("You are an adult <br />");
		if(age<18)
		document.write("You are NOT an adult <br />");
	</script>
</head>
<body>
</body>
</html>

## If…Else statement

**Syntax:**

if(condition)
{
	lines of code to be executed if the condition is true
}
else
{
	lines of code to be executed if the condition is false
}

You can use `If….Else` statement if you have to check two conditions and execute a different set of codes.

**Try this yourself:**

<html>
<head>
	<title>If...Else Statments!!!</title>
	<script type="text/javascript">
		// Get the current hours
		var hours = new Date().getHours();
		if(hours<12)
		document.write("Good Morning!!!<br />");
		else
		document.write("Good Afternoon!!!<br />");
	</script>
</head>
<body>
</body>
</html>

## If…Else If…Else statement

**Syntax:**

if(condition1)
{
	lines of code to be executed if condition1 is true
}
else if(condition2)
{
	lines of code to be executed if condition2 is true
}
else
{
	lines of code to be executed if condition1 is false and condition2 is false
}

You can use `If….Else If….Else` statement if you want to check more than two conditions.

**Try this yourself:**

<html>
<head>
	<script type="text/javascript">
		var one = prompt("Enter the first number");
		var two = prompt("Enter the second number");
		one = parseInt(one);
		two = parseInt(two);
		if (one == two)
			document.write(one + " is equal to " + two + ".");
		else if (one<two)
			document.write(one + " is less than " + two + ".");
		else
			document.write(one + " is greater than " + two + ".");
	</script>
</head>
<body>
</body>
</html>

This code is editable. Click Run to Execute  

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-use-conditional-statements-in-javascript.png","url":"https://www.guru99.com/images/how-to-use-conditional-statements-in-javascript.png","width":"484","height":"350","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/javascript","name":"JavaScript"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html","name":"Conditional Statements in JavaScript: if, else, and else if"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html#webpage","url":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html","name":"Conditional Statements in JavaScript: if, else, and else if","dateModified":"2024-02-24T15:00:01+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-use-conditional-statements-in-javascript.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"NewsArticle","headline":"Conditional Statements in JavaScript: if, else, and else if","keywords":"javascript, java","dateModified":"2024-02-24T15:00:01+05:30","articleSection":"JavaScript","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform anothe","copyrightYear":"2024","copyrightHolder":{"@id":"https://www.guru99.com/#organization"},"name":"Conditional Statements in JavaScript: if, else, and else if","@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html#webpage"},"image":{"@id":"https://www.guru99.com/images/how-to-use-conditional-statements-in-javascript.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html#webpage"}}]}
```
