Arithmetic operators
Arithmetic operators do math. On UrbanCart, you'll use them constantly โ calculating totals, applying discounts, computing tax, splitting payments.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 50 + 30 | 80 |
- | Subtraction | 200 - 45 | 155 |
* | Multiplication | 3 * 999 | 2997 |
/ | Division | 1500 / 3 | 500 |
% | Remainder (modulo) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 10 | 1024 |
A customer adds 3 wireless earbuds at 1299 each. UrbanCart gives a 10% bulk discount. Let's calculate the final total.
Comparison operators
Comparison operators compare two values and return true or false. They're the foundation of every decision your code makes.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Loose equality | "5" == 5 | true |
=== | Strict equality | "5" === 5 | false |
!= | Loose inequality | "5" != 5 | false |
!== | Strict inequality | "5" !== 5 | true |
> | Greater than | 1500 > 1000 | true |
< | Less than | 499 < 500 | true |
>= | Greater or equal | 1000 >= 1000 | true |
<= | Less or equal | 999 <= 1000 | true |
== operator does type coercion, which leads to bizarre bugs. "0" == false is true, "" == 0 is true, and null == undefined is true. Strict === checks both value AND type โ no surprises.
Logical operators
Logical operators combine multiple conditions. In UrbanCart, you'll chain conditions all the time: "Is the product in stock AND is the price under budget?"
| Operator | Name | Meaning |
|---|---|---|
&& | AND | Both sides must be true |
|| | OR | At least one side must be true |
! | NOT | Flips true to false, false to true |
&&, if the left side is false, it skips the right side entirely. With ||, if the left side is true, it skips the right side. This is useful for safe property access: user && user.name won't crash if user is null.
if / else
The if statement is how your code makes decisions. If a condition is true, run this block. Otherwise, do something else.
UrbanCart runs a promotion: orders over 1000 get 10% off, orders over 5000 get 20% off. Otherwise, no discount. Let's code this with an if/else chain.
cartTotal > 1000 first, a cart of 6000 would get 10% instead of 20%. Always check the most specific (largest) condition first.
Ternary operator
The ternary operator is a one-line shortcut for simple if/else decisions. It uses the syntax: condition ? valueIfTrue : valueIfFalse
switch statement
When you have many possible values for a single variable, switch is cleaner than a long if/else chain. UrbanCart uses switch for shipping methods, payment types, and order statuses.
break, JavaScript "falls through" to the next case and runs its code too. This is a common bug. The default case handles any value that doesn't match โ like an else for switch.
Nullish coalescing (??)
The ?? operator provides a default value when something is null or undefined. It's newer than || and more precise โ it only triggers on null/undefined, not on 0, "", or false.
|| (OR) for defaults
Treats 0, "", false, null, undefined all as "missing." Use when any falsy value should trigger the default.
?? (Nullish) for defaults
Only treats null and undefined as "missing." Use when 0, "", or false are valid values you want to keep.
Try it yourself โ Price calculator
Build a price calculator: if the price is over 1000, apply a 10% discount. Log the original price, discount amount, and final price. Click Run to see the output.
Try it yourself โ Shipping cost calculator
Use a switch statement to calculate shipping costs. Try changing the shippingMethod variable to "standard", "express", or "overnight" and run again.
Test your knowledge
"5" === 5 in JavaScript?% (modulo) operator return?const status = stock > 0 ? "Available" : "Sold out" do?break in a switch case?|| and ?? when the left operand is 0?Challenge: UrbanCart Checkout Logic
Combine everything from this lesson. Write a mini checkout calculator that:
- Takes a
cartTotal,memberStatus(boolean), andpromoCode(string or null) - Members get 15% off, non-members get 5% off
- If promoCode is
"URBAN50", add an extra 50 off the final price - Use
??to defaultpromoCodeto"NONE" - Use a ternary to set the shipping label: free if final total is over 1000, otherwise 99
- Log every step with
console.log
Paste your solution into the Try It Yourself editor above and run it!
You just learned to make code think.
Now build a full-stack career.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ