• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • null 空

    null特指对象的值未设置。它是 JavaScript 基本类型之一。

    语法

    null
    

    描述

    null是一个字面量,不像undefined,它不是全局对象的一个属性。null是表示缺少的标识,指示变量未指向任何对象。把null作为尚未创建的对象,也许更好理解。在 API 中,null常在返回类型应是一个对象,但没有关联的值的地方使用。

    // foo 不存在,它从来没有被定义过或者是初始化过:
    foo;
    "ReferenceError: foo is not defined"
    
    // foo 现在已经是知存在的,但是它没有类型或者是值:
    var foo = null; 
    foo;
    null
    

    nullundefined的不同点:

    当检测nullundefined时,注意相等(==)与全等(===)两个操作符的区别,前者会执行类型转换:

    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
    

    上篇:undefined

    下篇:globalThis