Using loose equality without understanding rules
== applies multi-step coercion that can produce surprising results across strings, numbers, booleans, null, and undefined.
Fix: Default to === and use == only in deliberate, well-understood cases.
Loading...
See how JavaScript type coercion works with an interactive visualization. Compare == vs === behavior, explore truthy/falsy values, and see edge cases with NaN, null, and undefined.
Loading visualization...
Type coercion is JavaScript's implicit or explicit conversion process between primitives, which affects equality, arithmetic, boolean checks, and many edge-case behaviors.
JavaScript operations often require operands of certain types. When values differ from expected types, JavaScript converts them according to specification rules.
Coercion can be explicit, such as Number(value), String(value), or Boolean(value), or implicit, such as string concatenation and loose equality.
Understanding coercion prevents bugs around null, undefined, empty strings, NaN, and comparison operators.
Interview framing: define Type Coercion in one sentence, then explain one concrete runtime behavior and one common pitfall with a short code example.
== applies multi-step coercion that can produce surprising results across strings, numbers, booleans, null, and undefined.
Fix: Default to === and use == only in deliberate, well-understood cases.
Some strings convert unexpectedly, such as Number('') and Number(' ') returning 0.
Fix: Validate and normalize input before numeric conversion.
Values like 0, '', and NaN are falsy but may represent valid user input.
Fix: Use explicit checks for nullish values or domain-specific conditions.
Common interview prompts with concise model answers.
No. NaN is the only JavaScript value that is not equal to itself. Use Number.isNaN to test it reliably.
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
console.log([] == false); // true (coercion)Loose equality triggers coercion steps that convert values before comparison, leading to unintuitive but spec-defined results.
It can be acceptable for specific patterns like value == null to match null or undefined, but use it intentionally and sparingly.
No. Non-empty strings are truthy, so Boolean('false') returns true.
Continue with these concepts to strengthen your mental model.
console.log("5" == 5);console.log("5" === 5);console.log(0 == false);console.log(0 === false);console.log("" == false);console.log("" === false);Step through the code to see coercion in action.
No output yet.