Introduction
In this article, we will learn about Conditional Statements in JavaScript. These statements are also called Selection Statements.
The Conditional Statements in JavaScript:
- If Statement
- If Else Statement
- Elseif Statement
- Switch Case Statement
If Statement
If Statements are used for the purpose of decision making; they evaluate the statement if the condition is true. To check if the given If condition is satisfied, If Statement is executed; otherwise the condition is false to exit the If condition.
Syntax
- If (condition)
- {
- Statement; //given condition is true execute the statement
- }
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>if Condition</title>
- </head>
- <body>
- <h1>Conditional Statement in JavaScript</h1>
- <script type="text/javascript">
- var percentage =(prompt("Enter Your Mark"))
- if (percentage >= 40)
- {
- document.write("You Mark is Pass"); //given condition is true execute the statement
- }
- </script>
- </body>
- </html>
Output


If else Statement
If else Statement is an improvement over if Statement. It evaluates the statement if the condition is true or false. In this statement, the given If condition is true means to execute the if Statement. On the other hand, if the condition is false it means to execute another block.
Syntax
- if (Condition)
- {
- Statement; //true
- }
- else
- {
- Statement; //false
- }
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>if Condition</title>
- </head>
- <body>
- <h1>Conditional Statement in JavaScript</h1>
- <script type="text/javascript">
- var percentage =(prompt("Enter Your Mark"))
- if (percentage >= 40)
- {
- document.write("You Mark is Pass"); //given condition is true execute the statement
- }
- else
- {
- document.write("You Mark is Fail"); //given condition is false execute the statement
- }
- </script>
- </body>
- </html>
Output

Else If Statement
It is another decision-making statement. It evaluates the Statement only if condition is true from several conditions, elseif statement can be used many times in a program; if the two conditions are false it means else Statement is executed.
Syntax
- If (condition 1)
- {
- statement 1; //true
- }
- elseif (condition 2)
- {
- statement 2; //true
- }
- else
- {
- statement; //evaluate if condition is false
- }
Example




Join the conversation! Your thoughts help the community grow.