zoukankan      html  css  js  c++  java
  • JavaScript typeof, null, 和 undefined

    typeof 操作符

    你可以使用 typeof 操作符来检测变量的数据类型。

    实例

    typeof "John"                // 返回 string 
    typeof 3.14                  // 返回 number
    typeof false                 // 返回 boolean
    typeof [1,2,3,4]             // 返回 object
    typeof {name:'John', age:34} // 返回 object

    尝试一下 »
    Note  在JavaScript中,数组是一种特殊的对象类型。 因此 typeof [1,2,3,4] 返回 object。 

    Null

    在 JavaScript 中 null 表示 "什么都没有"。

    null是一个只有一个值的特殊类型。表示一个空对象引用。

    Note 用 typeof 检测 null 返回是object。

    你可以设置为 null 来清空对象:

    实例

    var person = null;           // Value is null, but type is still an object

    尝试一下 »

    你可以设置为 undefined 来清空对象:

    实例

    var person = undefined;     // 值为 undefined, type is undefined

    尝试一下 »

    Undefined

    在 JavaScript 中, undefined 是一个没有设置值的变量。

    typeof 一个没有值的变量会返回 undefined

    实例

    var person;                  // Value is undefined, type is undefined

    尝试一下 »

    任何变量都可以通过设置值为 undefined 来清空。 类型为 undefined.

    实例

    person = undefined;          // 值为 undefined, type is undefined

    尝试一下 »

    Undefined 和 Null 的区别

    typeof undefined             // undefined
    typeof null                  // object
    null === undefined           // false
    null == undefined            // true
  • 相关阅读:
    【经典数据结构】B树与B+树
    【经典算法】线性时间排序
    【经典算法】归并排序
    【经典算法】快速排序
    python模块之shelve
    python模块之pickle
    python模块之json
    python之序列化
    python模块之shutil和zipfile
    python模块之sys
  • 原文地址:https://www.cnblogs.com/aiqingqing/p/4543058.html
Copyright © 2011-2022 走看看