Glossary / undefined

undefined

undefined is a primitive value that commonly means a binding has not been assigned, a function returned without a value, an argument was omitted, or a property lookup found no value. It is also possible for an existing property to explicitly contain undefined, so the value alone does not tell you whether a property is absent.

Where it comes from

let pending;
console.log(pending); // undefined

function save(value) {}
save(); // value is undefined

const record = {};
console.log(record.missing); // undefined

undefined is the value of an uninitialized var binding and the default return value of a function without an explicit return. A let or const binding before initialization is different: reading it throws a TDZ ReferenceError rather than yielding undefined.

Missing versus present-with-undefined

const a = {};
const b = { token: undefined };

a.token === b.token; // true
Object.hasOwn(a, "token"); // false
Object.hasOwn(b, "token"); // true

This distinction matters for patch APIs, configuration merging, serialization, and validation. JSON serialization omits object properties whose values are undefined, while undefined array entries are serialized as null. Destructuring and default parameters apply defaults to undefined, but not to null.

The global undefined value is non-writable in modern environments, though local bindings can technically shadow the name. Do not use undefined as an application-level sentinel if it is a legitimate data value; use an explicit tagged state or presence check.

Engineering guidance

Use optional properties intentionally and distinguish “not supplied” from “explicitly cleared” in APIs. Prefer Object.hasOwn when presence matters. Treat external objects as untrusted shapes and validate them. Use nullish coalescing when both null and undefined mean absence, but be clear about whether null should trigger a default.

Interview questions

  1. How can a function return undefined? It has no return statement, uses return;, or explicitly returns undefined.
  2. How distinguish a missing property from a present property whose value is undefined? Use Object.hasOwn or the in operator depending on whether inherited properties count.
  3. Does a default parameter apply to null? No; it applies to omitted or undefined arguments.
  4. How does JSON handle undefined? Object properties are omitted; array positions become null-like entries.

References