Nested ‘If’ Statements in JavaScript

This tutorial explains how to create nested if statements in JavaScript. A nested if statement is an if statement that is inside another if statement. Using nested if statements is not always the best way to write code but it is handy to know them anyway.

Watch the video below and then scroll down for the sample code.

The sample code below shows how to use if statements within other if statements (nested if statements).

Sample HTML code:

        <title>Nested if statements</title>
        <script src="script.js"></script>


Sample JavaScript code:

var spend = 120;

if(spend >= 20){
    if(spend >= 50){
        if(spend >= 100){
            console.log("Shipping is free!");
            console.log("Discounted shipping is $5");
        console.log("Discounted shipping is $7.50");
    console.log("Shipping is $10");

Next tutorial: Using and/or operators in if statements

Leave a Reply