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。

    undefined

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

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

    var person;                  // 值为 undefined(空), 类型是undefined

    undefined 和 null 的区别

    typeof undefined             // undefined
    typeof null                  // object
    null === undefined           // false
    null == undefined            // true

    null 和 undefined 的值相等,但类型不等

  • 相关阅读:
    .Net常见笔试题
    冒泡排序算法 C#版
    Bundle捆绑压缩技术
    异步Ajax
    HtmlHelper总结
    HtmlHelper的扩展分页方法
    WCF
    程序猿值得看的几个技术网站(记录)
    Struts2和SpringMVC的区别
    nginx配置文件作用介绍
  • 原文地址:https://www.cnblogs.com/gengyufei/p/12594743.html
Copyright © 2011-2022 走看看