The value
null
represents the intentional absence of any object value. It is one of JavaScript's
原语值
and is treated as
falsy
for boolean operations.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
null
The value
null
is written with a literal:
null
.
null
is not an identifier for a property of the global object, like
undefined
can be. Instead,
null
expresses a lack of identification, indicating that a variable points to no object. In APIs,
null
is often retrieved in a place where an object can be expected but no object is relevant.
// foo does not exist. It is not defined and has never been initialized: foo; //ReferenceError: foo is not defined
// foo is known to exist now but it has no type or value: var foo = null; foo; //null
null
and
undefined
When checking for
null
or
undefined
, beware of the
differences between equality (==) and identity (===) operators
, as the former performs type-conversion.
typeof null // "object" (not "null" for legacy reasons) typeof undefined // "undefined" null === undefined // false null == undefined // true null === null // true null == null // true !null // true isNaN(1 + null) // false isNaN(1 + undefined) // true
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'null value' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
null
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持