Saturday, February 15, 2020

Conditional Statements In JavaScript

Conditional/Branching Statements In JavaScript


Branching or Conditional statements execute the flow of the program on the basis of specified conditions. The conditions return Boolean (true or false) value upon execution.

In JavaScript, there are three conditional statements,

  • If Statement
  • If-else Statement
  • If-else-if Statement

"if" Statements

if statement JavaScript
if statement
The “if” statement is a basic branching statement. It is available almost in every programming language.

Syntax:
If (Condition)
{
//Statements to execute
}



"if-else" Statements

if-else statement JavaScript
if-else statement

The “if-else” statement has two paths, and one of them will be executed definitely in the execution of the program.

Syntax:
if(Condition)
{
//Statements to execute
}
else
{
//Statements to execute
}


“if-else-if” Statements

if-else-f statement JavaScript
if-else-if statement
The “if-else-if” statement has multiple paths of execution and one of these paths will be executed. We can check multiple conditions with “if-else-if”.


Syntax:
if(Condition)
{
//Statements to execute
}
else-if
{
//Statements to execute
}
else-if 
{ 
//Statements to execute 
} 
else 
{ 
//Statements to execute 
}