Nested If Statement in JavaScript


Nested "if" statement is an "if" statement that is placed inside another "if" or "else" block. It allows you to create more complex decision-making logic by handling multiple conditions and their respective code blocks.

Syntax:
if (outerCondition)
{
    Code to be executed if outerCondition is true
     if (innerCondition1) {
        // Code to be executed if innerCondition1 is true
     } else {
        // Code to be executed if innerCondition1 is false
     }
     if (innerCondition2) {
        // Code to be executed if innerCondition2 is true
     } else {
        // Code to be executed if innerCondition2 is false
     }
} else {
    Code to be executed if outerCondition is false
}


Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

script.js
//Nested If Statement
/*
if(cond)
{
  if(cond)
  {
    ---
  }
}

min mark >=35
  91-100 A
  81-90 B
  71-80 C
  D
*/

let english=95,tamil=98,maths=75;
let total,avg;
total=english+tamil+maths;
avg=total/3;
console.log("Total   : "+total);
console.log("Average : "+avg.toFixed(2));

if(english>=35 && tamil>=35 && maths>=35)
{
  console.log("Result  : Pass");
  if(avg>90 && avg<=100){
    console.log("Grade   : A Grade");
  }else if(avg>80 && avg<=90){
    console.log("Grade   : B Grade");
  }else if(avg>70 && avg<=80){
    console.log("Grade   : C Grade");
  }else{
    console.log("Grade   : D Grade");
  }
}
else
{
  console.log("Result  : Fail");
  console.log("Grade   : No Grade");
}
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project