undefined vs null in JavaScript
undefined and null both mean 'no value' but for different reasons. Here is the difference and when to use each.
undefined vs null in JavaScript
undefined and null both represent "no value," but they have different meanings and origins.
undefined: The Engine's Default
undefined is what the engine assigns when a variable has no value:
- Declared but unassigned variable.
- Missing function parameter.
- Function with no
return. - Missing object property.
let x; console.log(x); // undefined function foo(a) { return a; } console.log(foo()); // undefined const obj = {}; console.log(obj.missing); // undefined
null: The Developer's Intent
null is what you assign when you intentionally want to say "no value":
let user = null; // no user yet `` Use `null` to explicitly clear a value or signal "intentionally empty." ### Key Differences | Feature | `undefined` | `null` | |---|---|---| | Origin | Engine default | Developer assignment | | `typeof` | `"undefined"` | `"object"` (historical bug) | | Loose equality (`==`) | equal to `null` | equal to `undefined` | | Strict equality (`===`) | not equal to `null` | not equal to `undefined` | | In JSON | `JSON.stringify` omits it | `JSON.stringify` includes `null` | | Numeric coercion | `NaN` | `0` | ### Numeric Coercion Surprise ```js Number(undefined); // NaN Number(null); // 0 `` This means `null + 5` is `5`, but `undefined + 5` is `NaN`. This is a real source of bugs. ### JSON Behavior ```js JSON.stringify({ a: undefined, b: null }); // '{"b":null}' `` `undefined` properties are omitted from JSON. `null` is preserved. This matters for API payloads. ### When to Use Which - Use `undefined` for "not yet set" (let the engine assign it; check with `=== undefined`). - Use `null` for "intentionally no value" (assign it yourself). - Use `== null` to catch both in one check. ### The Takeaway `undefined` is the engine's default for missing values. `null` is the developer's explicit "no value." `typeof null` is `"object"` (a bug). `null` coerces to `0`, `undefined` to `NaN`. JSON omits `undefined` but keeps `null`. Use `null` for intentional emptiness.
undefined is the engine's default for variables and properties with no value. null is a value the developer assigns to intentionally mean 'no value.' typeof undefined is 'undefined'; typeof null is 'object' (a historical bug).
It is a historical bug from the original implementation. null was represented with a type tag of 0, which the typeof check interpreted as object. It cannot be fixed without breaking existing code, so it remains.
Number(undefined) is NaN, so undefined + 5 is NaN. Number(null) is 0, so null + 5 is 5. This difference is a real source of bugs in arithmetic.
JSON.stringify omits properties with undefined values, but preserves properties with null values as null. This matters for API payloads where the presence of a null key has meaning.
Use null when you intentionally want to say 'no value here' (e.g., resetting a variable). Let undefined be the engine's default for things not yet assigned. Use == null to check for both at once.
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.

