Javascript Nested conditionals
Nested conditional in conditional statement in JavaScript programming means that JavaScript programmers can create basic to advanced programming condition situations by placing if, else if, or else control flow statements inside another existing if, else if, or else block statement as per the programming condition. JavaScript control flow statement structure helps in creating complex programming conditions by testing and analyzing multiple programming conditions in order.

Syntax of JavaScript nested conditional.
JavaScript programmers can create and nest if, else if, and else control flow statements inside each other.
Syntax of JavaScript nested conditional.
if (ifexpression1) {
//The code for the first condition1 is the program code block
if (ifexpression2) {
//The program source code block for the second ifexpression condition2 is the condition (nested inside condition1)
} else {
/The code block is executed when condition2 is false in the nested control flow condition.
}
} else {
/The else condition will be executed when condition1 is false and the code block will run.
}
Example of nested conditional statement in JavaScript.
Here we create a program where we have to check the adulthood of a person based on his age and whether he is an adult or not.
let age = 21;
let hasmature = true;
if (age >= 19) {
if (hasmature) {
console.log(“You are mature!”);
} else {
console.log(“You are not mature.”);
}
} else {
console.log(“You are minor.”);
}
Explanation of the given program.
Here first condition checks whether the person is 19 years or older where age is already (age >= 19) years.
If the person is 19 years or older, then the program implements the condition whether the person is an adult or not.
Here if both the conditions are true, then the program prints the statement “You are an adult!”.
If the person is not an adult, then it prints the statement “Then you are not an adult”.
If the person is below 19 years of age, then the program prints the statement “You are a minor”.
Checking multiple conditions in nested conditionals example.
Here, let’s assume that we want to display the weight and fitness level of a person by dividing it by his weight and fitness habits.
Here, you get the weight balance fitness levels as follows:
“Excellent” if the person is 50 years or less overweight and exercises regularly.
“Good” if the person is overweight but exercises regularly.
“Needs improvement” if the person is underweight and does not exercise.
“Poor” if the person is overweight and does not exercise.
let weight = 70;
let balanceeweight = false;
if (weight > 50) {
if (balanceeweight) {
console.log(“Fitness level is Excellent”);
} else {
console.log(“Fitness level, you Needs improvement”);
}
} else {
if (balanceeweight) {
console.log(“Fitness level is Good”);
} else {
console.log(“Fitness level is poor”);
}
}
In the explanation of this program.
Here the first condition checks if the person is 50 years or less overweight. If the condition is true, then it tests the next condition that whether the person exercises regularly or not.
If both the conditions are true, then it prints the message “Fitness level: Excellent”.
If the person does not exercise, then it prints the message “Fitness level: Needs improvement”.
If the person is overweight or more than 50 years, then here the program checks if they exercise regularly.
If they do, then it prints the message “Fitness level: Good”.
Otherwise, it prints the message “Fitness level: Poor”.
Example of nested else if and else statements in JavaScript.
JavaScript programmers can preview else if and else statements by nesting them inside each other to efficiently manage multiple program control flow statement conditions.
let number = 85;
if (number >= 90) {
console.log(“Grade -> A”);
} else if (number >= 75) {
if (number >= 80) {
console.log(“Grade -> B+”);
} else {
console.log(“Grade -> B”);
}
} else {
console.log(“Grade -> C”);
}
Explanation of the above program.
Here the program first checks whether the number is greater than or equal to 90. If this statement is true, then it prints the message “Grade -> A”.
If the numbers here are between 75 and 89, then it checks whether the numbers are greater than or equal to 80. If it is true, then it prints the message “Grade -> B+”; otherwise, it prints the message “Grade: -> B”.
If the numbers here are less than 75, then it prints the message “Grade -> C”.
Example of using nested if statements with logical operators.
You can combine nested conditionals for more complex testing by applying logical operators (such as && for AND, || for OR) to a JavaScript program.
let weather = 48;
let isgrow = false;
if (weather > 40) {
if (isgrow) {
console.log(“It’s hot and temperature grow, stay in house!”);
} else {
console.log(“It’s very hot outside, stay cool at home!”);
}
} else {
if (isgrow) {
console.log(“weather is cold but grow,”);
} else {
console.log(“The weather is nice, you can go outside!”);
}
}
Explanation of the above program.
This program first checks whether the weather temperature is above 40 or not.
If true, then it checks whether the weather is hot or not.
If the weather is getting hot, then it is advisable to stay indoors. If the weather is not increasing, then it is advisable to stay at home. If the weather is 40 or less, then it checks whether the weather is getting hot or not. If the weather is hot, then it is increasing slowly. If the weather is not increasing, then you can go out and enjoy the good weather.
Conclusion of Nested Statements in JavaScript Programming.
Nested conditional statements in any JavaScript program help the control flow programmer to make more complex decisions in their custom program code. Where you create one nested condition inside another, you can test multiple nested condition flows with a structural order. However, sometimes nesting the program source code too deeply can make your program code more complex to read and maintain, so use it only when needed, and it is a good option to keep the program as simple as possible and use it only when necessary.
Learn before using nested control flow statements in JavaScript.
- Indentation – Always indent your code properly in the proper order to maintain the readability score in any JavaScript program.
- Complexity – Always avoid applying deeply nested conditionals to a simple program, as these steps can make your program source code complex to understand. If needed, display the program source logic code by breaking it into separate functions if necessary.
- Logical operators – If needed, you can use logical operators (&&, ||) to create easy program conditions.