You are currently viewing Conditional Statements in JavaScript

Conditional Statements in JavaScript

Conditional Statements in JavaScript

Conditional Statements

Conditional Statements are the statements that control the behavior of the JavaScript code. With the help of certain conditions, we can control the execution of some lines of code. If a certain condition is fulfilled then only a certain piece of code will run.

Types of Conditional Statements

We have three types of Conditional Statements in JavaScript.

  • if statement
  • if else statement
  • if.. else-if… else statement

if Statement

The if block will only run if the condition is true. Otherwise, it will execute the next statement after if block.

Syntax

if(Condition/Expression)
{
// statement(s)
}

Flowchart

Example

let a = 5;
if (a>10)
{
console.log("The no. is greater then 10 ");// This line will execute if a is greater then 10 
}
console.log("The next statement ");//This is the next statement after if block ,if the condition is false then this line will execute.

if else statement

If the condition is true then if block will execute ,otherwise else block will execute.

Syntax

if (Condition)
{
//Body of if block
}
else
{
//Body of else block
}

Flowchart

Example

let a = 5;
if (a>10)
{
console.log("The no. is greater then 10 ");// execute if condition is true 
}
else
{
console.log("The no. is not greater then 10");// execute if condition is false
}

if.. else-if… else statement

In ,if.. else-if… else we have multiple conditions. If the if block condition is false then it will check for the else if block condition and so on the process goes on.

Syntax

if (condition1)
{
//Body of if block
}
else if (condition2)
{
//Body of else if block
}
else
{
//Body of else block
}

Flowchart

Example

let age = 18;
if (age>18)
{
console.log("Age is less then 18");
}
else if(age==18)
{
console.log("Age is 18");
}
else
{
console.log("Age is greater then 18");
}

Check all previous tutorials here JAVASCRIPT TUTORIAL

Article Contributed By :聽Ankita Kataria.

Want to learn about DevOps technology then you must check this DevOps Beginner Guide.

This Post Has 2 Comments

Leave a Reply