--- title: 'null' slug: Web/JavaScript/Reference/Global_Objects/null tags: - JavaScript - Literal - Primitive translation_of: Web/JavaScript/Reference/Global_Objects/null --- <div>{{jsSidebar("Objects")}}</div> <p>值 <code>null</code> 特指对象的值未设置。它是 JavaScript {{Glossary("Primitive", "基本类型")}} 之一,在布尔运算中被认为是<a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">falsy</a>。</p> <div>{{EmbedInteractiveExample("pages/js/globalprops-null.html")}}</div> <h2 id="语法">语法</h2> <pre class="syntaxbox">null</pre> <h2 id="描述">描述</h2> <p>值 <code>null</code> 是一个字面量,不像 {{jsxref("Global_Objects/undefined","undefined")}},它不是全局对象的一个属性。<code>null</code> 是表示缺少的标识,指示变量未指向任何对象。把 <code>null</code> 作为尚未创建的对象,也许更好理解。在 API 中,<code>null</code> 常在返回类型应是一个对象,但没有关联的值的地方使用。</p> <pre class="brush: js">// foo 不存在,它从来没有被定义过或者是初始化过: foo; "ReferenceError: foo is not defined" // foo 现在已经是知存在的,但是它没有类型或者是值: var foo = null; foo; null</pre> <h3 id="null_与_undefined_的不同点:"><code>null</code> 与 <code>undefined</code> 的不同点:</h3> <p>当检测 <code>null</code> 或 <code>undefined</code> 时,注意<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">相等(==)与全等(===)两个操作符的区别</a> ,前者会执行类型转换:</p> <pre class="brush: js">typeof null // "object" (因为一些以前的原因而不是'null') 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 </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范版本</th> <th scope="col">规范状态</th> <th scope="col">注解</th> </tr> <tr> <td>{{SpecName('ES1')}}</td> <td>{{Spec2('ES1')}}</td> <td>Initial definition.</td> </tr> <tr> <td>{{SpecName('ES5.1', '#sec-4.3.11', 'null value')}}</td> <td>{{Spec2('ES5.1')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ES6', '#sec-null-value', 'null value')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-null-value', 'null value')}}</td> <td>{{Spec2('ESDraft')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("javascript.builtins.null")}}</p> <h2 id="参见">参见</h2> <ul> <li>{{jsxref("undefined")}}</li> <li>{{jsxref("NaN")}}</li> </ul>