JS if else

Last Updated: March 16, 2022

JS if statement is used to execute the block of code based on the condition

1 if statement

2 if else statement

3 if else if statement

1 if statement

If the condition is true the block of code will execute

Syntax

if (condition) {
  //  if condition is true , block of code will execeute
} 

Example

If marks are greater than 75 grade is equal to “A Pass”

if (marks > 75) {
  grade = "A Pass";
} 

2 if else statement

if (condition) {
  //  if condition is true , block of code will execeute
} else{
  //  if condition is false , block of code will execeute
}
if (marks > 75) {
  grade = "A Pass";
} else{
  grade = "Normal Pass";
}

3 if else if statement

if (condition1) {
  //  if condition1 is true , block of code will execeute
} else if(condition2){
  //  if condition2 is true , block of code will execeute
} else{
  // if both conditions are false this block of code will execute
}
Test