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

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

    一、typeof示范代码

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

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

    用 typeof 检测 null 返回是object。

    var person = null;           // 值为 null(空), 但类型为对象

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

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

    四、undefined 和 null 的区别

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

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

    五、可通过 instanceof 操作符来判断对象的具体类型,语法格式

    arr = [1,2,3];
    if(arr instanceof Array){
        document.write("arr 是一个数组");
    } else {
        document.write("arr 不是一个数组");
    }

    本文参考:

    https://www.runoob.com/js/js-typeof.html

  • 相关阅读:
    nginx-syslog
    loki
    idea安装中文插件
    nginx虚拟目录alias
    个人 软件系统整理
    Python 遍历Sheet 每个Sheet都单独保存为一个Excel
    SQL Server 多表关联的update语句
    电商 生意参谋 抓取 访客数据 JS版/谷歌插件版
    EF 多表关联
    个人 圈外同学 对比分析
  • 原文地址:https://www.cnblogs.com/nayitian/p/14964646.html
Copyright © 2011-2022 走看看