Nested if statements JavaScript | Simple example code

Nested if statements JavaScript used to get multiple possible outcomes. the if-else statement only supports two blocks but it can be used as many times as you like to increase the possible outcomes.

Nested if statements JavaScript example code

Nesting if statements meaning using if or if-else inside another if or else statement. let’s See HTML example code test score grade:-

<!DOCTYPE html>
<html>

<head>
    <script>
        let sum = 91;
        if (sum >= 90){
            console.log("A Grade");
        } else if (sum > 35){
            if (sum >= 80){
                console.log("B Grade");
            } else if (sum >= 70){
                console.log("C Grade")
            } else {
                console.log("D Grade")
            }
        } else {
            console.log("Failed")
        }
    </script>

</head>
<body>

</body>
</html>

Output:

Nested if statements JavaScript HTML

Note: Avoid using more than three levels of nested if statements. Instead, use a switch case statement.

Do comment if you have any doubts and suggestion on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.