此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
null
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
In this article
尝试一下
function getVowels(str) { const m = str.match(/[aeiou]/gi); if (m === null) { return 0; } return m.length;}console.log(getVowels("sky"));// Expected output: 0语法
js
null描述
值null 被写作字面量:null。不像undefined,null 不是全局对象的属性。相反,null 是表示缺少的标识,指示变量未指向任何对象。在 API 中,null 常在预期的值应是一个对象,但又没有关联的对象的地方使用。
js
// foo 不存在。它从未被定义或初始化:foo; //ReferenceError: foo is not definedjs
// 已知 foo 现在已经存在,但它没有类型和值:const foo = null;foo; //null示例
>null 与undefined 的不同点
当检查值是否为null 或undefined 时,请注意相等(==)与全等(===)运算符的区别,前者会执行类型转换:
js
typeof null; // "object"(因历史原因而不是 "null")typeof undefined; // "undefined"null === undefined; // falsenull == undefined; // truenull === null; // truenull == null; // true!null; // trueNumber.isNaN(1 + null); // falseNumber.isNaN(1 + undefined); // true规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-null-value> |