null

null 值表示有意缺少任何对象值。它是 JavaScript 的 原始值 之一,对于布尔运算被视为 falsy

¥The null value represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

Try it

语法

¥Syntax

js
null

描述

¥Description

null 用文字书写:nullnull 不像 undefined 那样是全局对象属性的标识符。相反,null 表示缺乏标识,表明变量不指向任何对象。在 API 中,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.

js
// foo does not exist. It is not defined and has never been initialized:
foo; //ReferenceError: foo is not defined
js
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null

示例

¥Examples

nullundefined 之间的区别

¥Difference between null and undefined

检查 nullundefined 时,请注意 相等 (==) 和恒等 (===) 运算符之间的区别,因为前者执行类型转换。

¥When checking for null or undefined, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion.

js
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
Number.isNaN(1 + null); // false
Number.isNaN(1 + undefined); // true

规范

Specification
ECMAScript Language Specification
# sec-null-value

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also