0% found this document useful (0 votes)
242 views

JavaScript Conditional Statements - IF, Else, Else IF (Example)

This document discusses different types of conditional statements in JavaScript, including if statements, if/else statements, and if/else if/else statements. It provides the syntax for each statement type and interactive code examples to demonstrate their usage. The if statement executes code if a single condition is true. The if/else statement checks two conditions and executes different code blocks based on whether the condition is true or false. The if/else if/else statement checks multiple conditions and different code blocks are executed depending on which conditions are true.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views

JavaScript Conditional Statements - IF, Else, Else IF (Example)

This document discusses different types of conditional statements in JavaScript, including if statements, if/else statements, and if/else if/else statements. It provides the syntax for each statement type and interactive code examples to demonstrate their usage. The if statement executes code if a single condition is true. The if/else statement checks two conditions and executes different code blocks based on whether the condition is true or false. The if/else if/else statement checks multiple conditions and different code blocks are executed depending on which conditions are true.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/24/2021 JavaScript Conditional Statements: IF, Else, Else IF (Example)

JavaScript Conditional Statements: IF, Else, Else


IF (Example)
In this tutorial, you will learn-

How to use Conditional Statements


Different Types of Conditional Statements
If statement
If…Else statement
If…Else If…Else statement

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.

Different Types of Conditional Statements


There are mainly three types of conditional statements in JavaScript.
https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html 1/4
7/24/2021 JavaScript Conditional Statements: IF, Else, Else IF (Example)

1. If statement
2. If…Else statement
3. If…Else If…Else statement

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:

This code is editable. Click Run to Execute

1 <html>
2 <head>
3 <title>IF Statments!!!</title>
4 <script type="text/javascript">
5 var age = prompt("Please enter your age");
6 if(age>=18)
7 document.write("You are an adult <br />");
8 if(age<18)
9 document.write("You are NOT an adult <br />");
10 </script>
11 </head>
12 <body>
13 </body>
14 </html>

Run

If…Else statement
Syntax:

https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html 2/4
7/24/2021 JavaScript Conditional Statements: IF, Else, Else IF (Example)

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:

This code is editable. Click Run to Execute

1 <html>
2 <head>
3 <title>If...Else Statments!!!</title>
4 <script type="text/javascript">
5 // Get the current hours
6 var hours = new Date().getHours();
7 if(hours<12)
8 document.write("Good Morning!!!<br />");
9 else
10 document.write("Good Afternoon!!!<br />");
11 </script>
12 </head>
13 <body>
14 </body>
15 </html>

Run

If…Else If…Else statement


Syntax:

https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html 3/4
7/24/2021 JavaScript Conditional Statements: IF, Else, Else IF (Example)

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:

This code is editable. Click Run to Execute

1 <html>
2 <head>
3 <script type="text/javascript">
4 var one = prompt("Enter the first number");
5 var two = prompt("Enter the second number");
6 one = parseInt(one);
7 two = parseInt(two);
8 if (one == two)
9 document.write(one + " is equal to " + two + ".");
10 else if (one<two)
11 document.write(one + " is less than " + two + ".");
12 else
13 document.write(one + " is greater than " + two + ".");
14 </script>
15 </head>
16 <body>
17 </body>
18 </html>
Run

https://www.guru99.com/how-to-use-conditional-statements-in-javascript.html 4/4

You might also like