Operators, Conditionals and Truthy Values
Comparison: == vs ===
0 == "0" // true (converts types first -- avoid this)
0 === "0" // false (strict, checks type too -- use this)
1 === 1 // true
"a" === "a" // true
Always use === and !== in JavaScript, never == and !=. The loose versions perform confusing automatic type conversion before comparing — 0 == "0", 0 == false, and even "" == 0 are all true, which causes real bugs. This is considered a mistake in JavaScript’s original design, and the strict versions avoid it entirely.
if / else if / else
const score = 82;
let grade;
if (score >= 90) {
grade = "A";
} else if (score >= 75) {
grade = "B";
} else {
grade = "C";
}
Truthy and falsy
In a boolean context — like the condition of an if statement — JavaScript converts any value to true or false automatically. Exactly six values are falsy: 0, "" (empty string), null, undefined, NaN, and false itself. Everything else, including an empty array [] and an empty object {}, is truthy. This is why you’ll often see:
if (userInput) {
// runs only if userInput is not empty/null/undefined/0
}
const name = userInput || "Guest"; // falls back to "Guest" if userInput is falsy
The ternary operator
const status = age >= 18 ? "adult" : "minor";
A ternary is a compact if/else that produces a value directly — useful for short conditional assignments, but resist nesting more than one ternary inside another, since that quickly becomes unreadable.