Operators
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
| Operator | Meaning |
|---|---|
| === | 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 |
== 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!)
"0" and [], is truthy, full stop.
if / else if / else
Note else if is always two words in JavaScript — unlike PHP, there's no single-word elseif equivalent here.
The Ternary Operator
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
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
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
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 solutionWrite 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 solutionWrite 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 solutionChapter 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