Arithmetic operators

Arithmetic operators do math. On UrbanCart, you'll use them constantly โ€” calculating totals, applying discounts, computing tax, splitting payments.

OperatorNameExampleResult
+Addition50 + 3080
-Subtraction200 - 45155
*Multiplication3 * 9992997
/Division1500 / 3500
%Remainder (modulo)10 % 31
**Exponentiation2 ** 101024
๐Ÿ›’
UrbanCart Example
Calculate a product total with discount

A customer adds 3 wireless earbuds at 1299 each. UrbanCart gives a 10% bulk discount. Let's calculate the final total.

arithmetic.js Product total with discount
Console output
Subtotal: 3897 Discount: 389.7 Total: 3507.3 Items on last page: 7

Comparison operators

Comparison operators compare two values and return true or false. They're the foundation of every decision your code makes.

OperatorMeaningExampleResult
==Loose equality"5" == 5true
===Strict equality"5" === 5false
!=Loose inequality"5" != 5false
!==Strict inequality"5" !== 5true
>Greater than1500 > 1000true
<Less than499 < 500true
>=Greater or equal1000 >= 1000true
<=Less or equal999 <= 1000true
Always use === (strict equality). The loose == 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.
comparison.js Why === matters
Console output
true true true false false false Free shipping? true

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?"

OperatorNameMeaning
&&ANDBoth sides must be true
||ORAt least one side must be true
!NOTFlips true to false, false to true
logical.js Stock checks and budget filters
Console output
Can buy? true Gets discount? true Out of stock? false Eligible? true
Short-circuit evaluation: JavaScript is lazy. With &&, 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 Example
Apply discount if cart total exceeds 1000

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.

discount.js if / else if / else chain
Console output
Standard discount: 10% off! Cart total: 2500 You save: 250 Final total: 2250
Order matters. JavaScript checks conditions top to bottom and stops at the first match. If you put 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

ternary.js One-line conditional
Console output
Status: Available Free Shipping Discount rate: 15% Grade: B
Don't overuse ternaries. They're great for simple true/false assignments. For complex logic with multiple branches, stick with if/else โ€” your future self (and your team) will thank you.

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.

shipping.js Shipping method selector
Console output
Method: express Cost: 249 Delivery: 2-3 business days
Don't forget break! Without 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.

nullish.js Safe defaults with ??
Console output
Discount: 0 Welcome, Guest Shopper 10 0 No notes
โšก

|| (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 โ€” Price Calculator with Discounts
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.

โœ๏ธ Try It Yourself โ€” Shipping Cost Calculator
Output

Test your knowledge

Question 1 of 5
What is the result of "5" === 5 in JavaScript?
Question 2 of 5
What does the % (modulo) operator return?
Question 3 of 5
What does const status = stock > 0 ? "Available" : "Sold out" do?
Question 4 of 5
What happens if you forget break in a switch case?
Question 5 of 5
What is the difference between || and ?? when the left operand is 0?
0
/5
Great job!

Challenge: UrbanCart Checkout Logic

Combine everything from this lesson. Write a mini checkout calculator that:

  • Takes a cartTotal, memberStatus (boolean), and promoCode (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 default promoCode to "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!

Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’