JavaScript Weakly Typed Language and undefined Behavior
JS is weakly typed, and undefined participates in coercion. Here is how undefined behaves in operations and comparisons.
JavaScript Weakly Typed Language and undefined Behavior
JavaScript is weakly typed, which means it coerces values automatically. undefined is a value, and it has specific coercion rules that often cause bugs.
undefined in Arithmetic
undefined + 1; // NaN undefined - 1; // NaN undefined * 2; // NaN undefined / 2; // NaN
undefined coerces to NaN in numeric contexts. Any arithmetic with NaN produces NaN, which propagates silently.
undefined in String Concatenation
"Value: " + undefined; // "Value: undefined" `1 + undefined`; // "1undefined"
The + operator favors strings. undefined becomes the string "undefined".
undefined in Equality
undefined == undefined; // true undefined == null; // true (loose equality special case) undefined === undefined; // true undefined === null; // false
undefined and null are loosely equal to each other but not to any other value. Use === to avoid surprises.
undefined in Boolean Context
undefined is falsy:
if (undefined) { /* not reached */ } !undefined; // true Boolean(undefined); // false
But beware if (!x): it also catches 0, "", null, false, and NaN.
undefined in Default Parameters
Default parameters apply when the argument is undefined (or omitted):
function foo(a = 5) { return a; } foo(undefined); // 5 (default applies) foo(null); // null (default does not apply) foo(0); // 0 (default does not apply)
This is why undefined is special in function signatures: it is the signal for "use the default."
undefined in Destructuring Defaults
const { name = "Anonymous" } = {}; console.log(name); // "Anonymous" const { age = 18 } = { age: undefined }; console.log(age); // 18 (default applies) const { score = 0 } = { score: null }; console.log(score); // null (default does not apply)
The Takeaway
undefined coerces to NaN in arithmetic, "undefined" in string concatenation, and is loosely equal only to null. It is falsy. It triggers defaults in parameters and destructuring. Knowing these rules prevents real bugs.
NaN. undefined coerces to NaN in numeric contexts, and any arithmetic with NaN produces NaN, which propagates silently through calculations.
'Value: undefined'. The + operator favors string concatenation, so undefined is coerced to the string 'undefined' and concatenated.
Yes. undefined == null is true, a special case in the spec. But undefined === null is false. They are only loosely equal to each other, not to any other value.
No. Defaults apply only when the argument is undefined or omitted. Passing null, 0, or empty string does not trigger the default, because those are real values.
Falsy. if (undefined) is not entered, !undefined is true, and Boolean(undefined) is false. But if (!x) also catches 0, '', null, false, and NaN, so use === undefined for precise checks.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

