This file covers the JavaScript type system, coercion rules, and equality behavior. These topics show up constantly in interviews because they expose whether someone understands how the language actually behaves.
Values have types, variables do not. Coercion is rule-based, not random. Equality rules matter because JavaScript performs implicit conversions in specific cases.
stringnumberbooleannullundefinedsymbolbigint
object
- Arrays, functions, and dates are all objects.
typeof nullis"object", which is a long-standing language bug.- Variables can hold values of different types over time.
false0-00n""nullundefinedNaN
Everything else is truthy.
An empty array [] and empty object {} are both truthy.
Coercion is automatic type conversion. It can be explicit or implicit.
String(42); // "42"
Number("42"); // 42
Boolean("hello"); // true"5" + 1; // "51"
"5" - 1; // 4
if ("hello") {
// truthy
}- Assuming
+always means numeric addition. - Forgetting that
-,*, and/coerce toward numbers. - Treating coercion as unpredictable instead of rule-based.
===checks value and type without coercion.==allows coercion before comparison.
42 === "42"; // false
42 == "42"; // true
null == undefined; // true
null === undefined; // falseI prefer === by default because it avoids implicit conversion surprises. I still understand == because it follows defined coercion rules and appears in real code.
Because the array is coerced to an empty string, then to 0, and false also becomes 0.
Because NaN is defined as a value that is not equal to anything, including itself.
Use Number.isNaN(value).
Objects are compared by reference, not by structural equality.
{} === {}; // false
const a = { x: 1 };
const b = a;
a === b; // trueThinking two objects with the same shape and values are equal with ===.
Seven primitive types and the object type.
The small set of values that coerce to false in boolean contexts.
Because it avoids implicit coercion and is easier to reason about.
Because it returns "object" due to a long-standing bug in the language.