JavaScript – null and undefined

null is a language keyword
that evaluates to a special value that is usually used to indicate the
absence of a value. Using the typeof operator on null returns the string “object”, indicating
that null can be thought of as a
special object value that indicates “no object”. In practice, however,
null is typically regarded as the
sole member of its own type, and it can be used to indicate “no value”
for numbers and strings as well as objects. Most programming languages
have an equivalent to JavaScript’s null: you may be familiar with it as
null or nil.

JavaScript also has a second value that indicates absence of
value. The undefined value represents a deeper kind of absence. It is
the value of variables that have not been initialized and the value
you get when you query the value of an object property or array
element that does not exist. The undefined value is also returned by
functions that have no return value, and the value of function
parameters for which no argument is supplied. undefined is a predefined global variable
(not a language keyword like null)
that is initialized to the undefined value. In ECMAScript 3, undefined is a read/write variable, and it
can be set to any value. This error is corrected in ECMAScript 5 and
undefined is
read-only in that version of the language. If you apply the typeof operator to the undefined value, it
returns “undefined”, indicating that this value is the sole member of
a special type.

Despite these differences, null and undefined both indicate an absence of value
and can often be used interchangeably. The equality operator == considers them to be equal. (Use the
strict equality operator === to
distinguish them.) Both are falsy values—they behave like false when a boolean value is required.
Neither null nor undefined have any properties or methods. In
fact, using . or [] to access a property or method of these
values causes a TypeError.

You might consider undefined
to represent a system-level, unexpected, or error-like absence of
value and null to represent
program-level, normal, or expected absence of value. If you need to
assign one of these values to a variable or property or pass one of
these values to a function, null is
almost always the right choice.

Comments are closed.