Start learning free

LESSON 187 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in JavaScript: Free Shipping with if/else

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

const subtotal = 75;
const shipping_cost = 10;
const free_shipping_threshold = 100;

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= free_shipping_threshold, set shipping to 0; otherwise set it to shipping_cost.
  • total = subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result, then print it with console.log(result).

Decide shipping in an if/else first, then build the three lines.

Declare let shipping; before the if, so it is still visible after the if/else, then assign it in both branches.

In JavaScript "Total: $" + total converts the number to text for you.

const subtotal = 75;
const shipping_cost = 10;
const free_shipping_threshold = 100;

let shipping;
if (subtotal >= free_shipping_threshold) {
    shipping = 0;
} else {
    shipping = shipping_cost;
}
const total = subtotal + shipping;
const line1 = "Subtotal: $" + subtotal;
const line2 = "Shipping: $" + shipping;
const line3 = "Total: $" + total;
const result = line1 + "\n" + line2 + "\n" + line3;
console.log(result);

STEP 1 OF 11
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in JavaScript, explained

    Problem overview

    The program starts with three numbers: a subtotal of 75, a flat shipping_cost of 10 and a free_shipping_threshold of 100. It prints a three-line receipt: the subtotal, the shipping and the total. Carts at or above the threshold ship for free, so their shipping line shows $0. The other lines stay in the receipt either way.

    With the starting values, the cart is below the threshold, so it pays shipping:

    Subtotal: $75
    Shipping: $10
    Total: $85

    The earlier versions of this project printed fixed receipts. This one makes its first decision: the value of shipping depends on the data.

    Approach: decide the shipping first, then build the text

    Everything after the shipping depends on it: the total adds it, and the middle line prints it. So the plan is to settle shipping with an if/else and only then compute total and the three lines. Writing the decision first means the rest of the program reads the same for every cart.

    The condition is subtotal >= free_shipping_threshold. It uses >=, not >, because a cart exactly on the threshold counts as free. With a subtotal of 100, the receipt shows Shipping: $0 and a total of 100. At 99 it shows Shipping: $10 and a total of 109.

    The threshold and the cost are variables, not numbers typed into the condition. Changing the shop's policy then means editing one line at the top, and the condition keeps reading as what it means: the subtotal reaches the threshold.

    Step by step

    1. Declare shipping with let and no value. It gets its value in one of two branches, so it can't be a const, which must be assigned where it is declared.
    2. Write the if. Its condition compares subtotal with the threshold using >=. Close the block straight away so the braces always match.
    3. Free case. Inside the if, assign 0 to shipping.
    4. Open the else. It runs whenever the condition is false. The brace that closes the if block is also where else goes, on the same line.
    5. Paid case. Inside the else, assign shipping_cost to shipping.
    6. Compute total. It is the subtotal plus the shipping. When shipping is free it adds 0, so the total equals the subtotal.
    7. Build the three lines. Each is a label followed by the number, joined with +.
    8. Join them into result. Two "\n" separators go between the three lines.
    9. Print result with one console.log.

    The solution in JavaScript

    const subtotal = 75;
    const shipping_cost = 10;
    const free_shipping_threshold = 100;
    
    let shipping;
    if (subtotal >= free_shipping_threshold) {
        shipping = 0;
    } else {
        shipping = shipping_cost;
    }
    const total = subtotal + shipping;
    const line1 = "Subtotal: $" + subtotal;
    const line2 = "Shipping: $" + shipping;
    const line3 = "Total: $" + total;
    const result = line1 + "\n" + line2 + "\n" + line3;
    console.log(result);

    Details worth knowing

    let shipping; declares a variable that holds undefined until it is assigned. Because it is declared outside the if, it is still visible after the block. A variable declared with let or const inside the braces would disappear at the closing brace, so declaring shipping there would make the later total line fail.

    When + has a string on one side, JavaScript converts the other side to text and joins them. That is why "Total: $" + total works with a number. Be careful with the order, though: "5" + 3 gives "53", while "5" - 3 gives 2, because - always works on numbers. The $ in the string is an ordinary character, because the string uses plain quotes and not backticks.

    The three lines are separated by "\n", which is the newline character. There is no newline after the last line, because separators go between lines, not after them.

    A shorter form

    Once you are comfortable with if/else, the same decision can be written as one expression with the ternary operator: const shipping = subtotal >= free_shipping_threshold ? 0 : shipping_cost;. It gives the same shipping, 10 for a subtotal of 75 and 0 for 100, and it lets shipping be a const. The lesson uses the longer form on purpose: with a statement in each branch, you can see which code runs in which case, and a branch can later hold more than one line.

    The two branches here assign the same variable. That is the usual shape of a two-way decision: whichever branch runs, shipping ends up with a value, so the lines below never have to ask which case happened.

    Edge cases and common mistakes

    • Using > instead of >=. A cart of exactly 100 would pay 10 for shipping.
    • Dropping the shipping line when it is free. The receipt always has all three lines, and the free case prints Shipping: $0.
    • Declaring shipping inside a branch. It would not exist after the closing brace.
    • Using = in the condition. if (subtotal = free_shipping_threshold) assigns, and since the program declares subtotal with const, running it throws an error. Comparisons use >=, === and the like.
    • Adding the threshold to the total instead of the shipping, or forgetting that total must be computed after the if/else, when shipping already has its value.

    Testing

    Change subtotal in the editor above and check the receipt. These are the results for three subtotals:

    subtotalShipping lineTotal line
    75Shipping: $10Total: $85
    99Shipping: $10Total: $109
    100Shipping: $0Total: $100
    120Shipping: $0Total: $120
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free