javaScript判断数据类型以及typeof和intanceof的差别

 
 
一、JS有6种数据类型:5种简单数据类型( Undefined,Null.Boolean,Number,String)、1种复杂数据类型 Object;

 二、JS提供两种方式判断数据的类型: typeof和intanceof;

 三、typeof返回结果包括:undefined,boolean,string,number,object,function

 1、返回undefined,表示这个值为定义

    var message;
    console.log(typeof message) //undefined  表示这个值未定义

    var message = true;
    console.log(typeof message) //boolean  表示这个值是布尔值

    var message = 'hello';
    console.log(typeof message) //string  表示这个值是字符串

    var message = 123;
    console.log(typeof message) //number 表示这个值是数值

    var message = {};
    console.log(typeof message) //object 表示这个值是对象

    var message = null;
    console.log(typeof message) //object 表示这个值为null

    var message = function(){
      console.log('hello')
    }
    console.log(typeof message) //function 表示这个值为函数
四、instanceoftypeof区别在于instanceof判断一个实例是否属于某种类型,返回true或者false
   function Person(name,age) {
      this.name = name;
      this.age = age;
   }
   var person1 = new Person('nike',23);
   console.log(person1 instanceof Object) ;//true;
   console.log(person1 instanceof Person) ;//true;

    

  

   

  

猜你喜欢

转载自blog.csdn.net/yaomengzhi/article/details/78296534