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.
let value; console.log(value); // undefined
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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
voidexpressions. - Testing:
value === undefinedworks for any declared variable. Usetypeof name === "undefined"when the name might not exist at all, becausetypeofdoes not throw for undeclared identifiers. undefinedis not a keyword. It is a global property, and a local variable can shadow it (let undefined = 1inside a function). Never do that;void 0always yields the real value.- Versus
null:null == undefinedistrue, but===tells them apart.typeof nullis"object",typeof undefinedis"undefined". - Default parameters apply only for
undefined, not fornullor0. The??operator treats bothnullandundefinedas "missing". - JSON drops it:
JSON.stringifyomits object properties whose value isundefinedand writesnullfor array items. - Property exists but is undefined:
"theme" in settingsistrueeven though the value isundefined. UseinorObject.hasOwn()to tell a missing property from an undefined one. - Reading properties of
undefinedthrows aTypeError. Optional chaining (a?.b) avoids that.
Browser support
undefined works in every browser and runtime.
Related
NaN,globalThis,void,typeof- Lessons: null and undefined, Nullish and optional chaining