Overview
Make decisions with if / else and the ternary operator, and learn why JavaScript developers compare with === instead of ==.
- if / else
- === compares value and type; == converts first
- The ternary: condition ? a : b
Short Course
if / else runs one branch or the other. The trap: == converts types
before comparing, so 0 == "" is true. Use ===. A ternary picks
between two values in one line.
0 == ""; // true (surprise!)
0 === ""; // false
const age = 20;
if (age >= 18) { console.log("adult"); } else { console.log("minor"); }
const label = age >= 18 ? "adult" : "minor"; // "adult"
Task
Write shippingCost(total) that returns 0 when the order total is 50 or
more and 4.99 otherwise, e.g. shippingCost(49.99) returns 4.99.
Test Suites
Pending
Next
Output