Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Global Functions and Values

The undefined value

Reference for JavaScript undefined: when it appears, how it differs from null, how to test for it with typeof, and common sources such as missing arguments.

undefined is the value JavaScript uses for "nothing has been assigned". A variable declared without a value, a missing function argument, a missing object property and a function without a return all produce it. It is one of the primitive types, and undefined is also the name of a read-only property of the global object.

Use null when you want to say "intentionally empty"; leave undefined for the language to produce.

JavaScript
let value;
console.log(value);   // undefined

Try it

JS Where undefined comes from

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS undefined and null

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
undefined
typeof value === "undefined"
value === undefined

Value: the single value of the Undefined type.

Property attributes: undefined on the global object is not writable, not enumerable and not configurable.

Usage notes

  • Common sources: uninitialized variables, missing arguments, missing properties or array elements, functions that return nothing, and void expressions.
  • Testing: value === undefined works for any declared variable. Use typeof name === "undefined" when the name might not exist at all, because typeof does not throw for undeclared identifiers.
  • undefined is not a keyword. It is a global property, and a local variable can shadow it (let undefined = 1 inside a function). Never do that; void 0 always yields the real value.
  • Versus null: null == undefined is true, but === tells them apart. typeof null is "object", typeof undefined is "undefined".
  • Default parameters apply only for undefined, not for null or 0. The ?? operator treats both null and undefined as "missing".
  • JSON drops it: JSON.stringify omits object properties whose value is undefined and writes null for array items.
  • Property exists but is undefined: "theme" in settings is true even though the value is undefined. Use in or Object.hasOwn() to tell a missing property from an undefined one.
  • Reading properties of undefined throws a TypeError. Optional chaining (a?.b) avoids that.

Browser support

undefined works in every browser and runtime.