zoukankan      html  css  js  c++  java
  • typeof与instanceof运算符

    typeof运算符用来判断某个变量的数据类型。typeof()返回值类型有如下几种:

    1.number :数值类型
    2.string :字符串类型
    3.boolean :布尔型
    4.function:函数
    5.object:对象,null
    6.undefined:undefined

    举个例子:

    var a,b = 34;
    var str = "hello world";
    console.log(typeof(a))
    console.log(typeof(b))
    console.log(typeof(str))

    结果:a:undefined;b:number;c:string
    例子2:

    var f = function g() {
    return 23;
    };
    console.log(typeof f); //function
    console.log(typeof f()); //number
    console.log(typeof g); //undefined
    console.log(typeof g());//报错
     

    instanceof运算符用于判断某个变量是否为制定类的实例,返回值是布尔类型,如果是返回true,否则返回false。

    例如:

    var a,b = [2,4];
    var str = "hello world";
    console.log(b instanceof Array);//true
    console.log(b instanceof Object);//true
    console.log(a instanceof Object);//false
    console.log(str instanceof Object);//false
     

  • 相关阅读:
    MySQL第七课
    MySQL第六课
    mysql第五课
    MySQL第四课
    MySQL第三课
    MYSQL第一课
    MYSQL第二课
    char、vchar、nvarchar 的区别
    SSRS Reporting Service安装与部署
    存储过程用法
  • 原文地址:https://www.cnblogs.com/changpuyi/p/8493348.html
Copyright © 2011-2022 走看看