Operators

Course 1 · Ch 3
Operators and Control Flow: if/else, switch, Ternary
Comparing values and making decisions — including JavaScript's own take on loose vs strict equality

Decisions in JavaScript will look immediately familiar coming from PHP — the syntax is nearly identical. The genuinely important new concept here is truthy and falsy values, and the strong recommendation to use === rather than == almost universally.

Comparison Operators

OperatorMeaning
===Strict equality — value AND type must match, no conversion
!==Strict inequality
==Loose equality — converts types before comparing (avoid)
!=Loose inequality (avoid)
> / < / >= / <=Greater/less than, and "or equal" variants
console.log(5 === "5"); // false — different types, no conversion console.log(5 == "5"); // true — "5" is converted to a number first console.log(0 == false); // true — surprising, and exactly why == is discouraged
Use === almost always — this is even more strongly recommended in JS than in PHP
JavaScript's == performs type coercion with rules that produce some genuinely surprising results (0 == false, "" == 0, null == undefined). This is the JavaScript equivalent of PHP Fundamentals Chapter 2's == vs === guidance, but the JS community treats it as even more of a hard rule — production JS code overwhelmingly uses ===/!== by default, reaching for == only in rare, deliberate cases.

Truthy and Falsy — Values Treated as true/false in a Condition

Falsy (treated as false)

false 0 "" (empty string) null undefined NaN

Truthy (everything else)

true 1, -1, 3.14 (any non-zero number) "hello", "0", " " (any non-empty string!) [] (an empty array — still truthy!) {} (an empty object — still truthy!)
if ("0") { console.log("This runs!"); // "0" is a non-empty STRING — truthy, even though it looks like zero } if ([]) { console.log("This also runs!"); // an empty array is still truthy }
The string "0" and an empty array [] are both genuinely truthy — a very common surprise
There is no special-casing for "looks like zero" or "looks empty" — only the exact six falsy values listed above are ever falsy. Every other value, including "0" and [], is truthy, full stop.

if / else if / else

const score = 75; if (score >= 90) { console.log("Grade: A"); } else if (score >= 70) { console.log("Grade: B"); } else { console.log("Grade: F"); }

Note else if is always two words in JavaScript — unlike PHP, there's no single-word elseif equivalent here.

The Ternary Operator

const age = 20; const status = age >= 18 ? "adult" : "minor"; console.log(status); // "adult"

Identical syntax and use case to PHP's ternary from Fundamentals Chapter 3 — a compact if/else for simple value assignments.

The Logical OR/AND "Default Value" Trick

function greet(name) { const displayName = name || "Guest"; // if name is falsy, use "Guest" instead console.log(`Hello, ${displayName}!`); } greet("Philip"); // "Hello, Philip!" greet(); // "Hello, Guest!" — name is undefined (falsy), so the fallback is used
?? is a safer alternative — only falls back for null/undefined, not every falsy value
const displayName = name ?? "Guest"; — the "nullish coalescing" operator only uses the fallback when the left side is specifically null or undefined, not for other falsy values like 0 or "". This avoids the surprising case where a genuinely valid value like 0 or an empty string gets incorrectly replaced by ||'s fallback. Covered fully in Intermediate Chapter 1.

switch

const day = "Wednesday"; switch (day) { case "Monday": console.log("Start of the week"); break; case "Wednesday": console.log("Midweek"); break; default: console.log("Just another day"); }

Behaves identically to PHP's switch (Fundamentals Chapter 3), including the same fall-through behaviour when break is omitted, and JavaScript's switch always compares using strict equality (===) internally — never the loose comparison rules from ==.

Coding Challenges

Challenge 1

Write five console.log() statements testing whether each of these values is truthy or falsy inside an if/else: 0, "", "false" (the string), null, and [] (empty array). Predict each result first, then verify.

📄 View solution
Challenge 2

Write a function describeTemperature(celsius) using if/else if/else that returns "Freezing", "Cold", "Mild", or "Hot" based on reasonable thresholds. Test it with four different values and console.log() each result.

📄 View solution
Challenge 3

Write a function getDiscount(customerType) using switch that returns 0.2 for "vip", 0.1 for "member", and 0 for anything else (default). Then write a function welcomeMessage(name) using the || fallback trick to default to "Guest" if name is falsy, and test both functions with a few different inputs.

📄 View solution

Chapter 3 Quick Reference

  • === / !== — strict comparison, use almost always; == / != — loose, avoid
  • Falsy values (only six): false, 0, "", null, undefined, NaN — everything else is truthy
  • "0" and [] are truthy — a very common surprise; only the exact six falsy values count
  • if / else if / else — else if is always two words in JS
  • Ternary: condition ? a : b
  • value || fallback — uses fallback for ANY falsy value; value ?? fallback — only for null/undefined (Intermediate Ch 1)
  • switch — same fall-through behaviour as PHP; always compares strictly internally
  • Next chapter: loops — for, while, for...of, for...in