JavaScript 几种数据类型

175 阅读1分钟

Js数据类型

  • JavaScript有其中基本数据类型,分别为5种简单数据类型,3种引用数据类型(复杂数据类型)。
    • 简单数据类型为:String、Number、Boolean、NULL、Undefined。
    • 引用数据类型为:Object、Function、Array

检测数据类型的方法

  • 检测某数据的数据类型,常用方法有两种,分别为:typeof、instanceof
    • typeof:用来检测5种简单数据类型,不能检测复杂数据类型。
    • instanceof:用来检测复杂数据类型。
    • 测试实例:
    console.log(typeof('abc')); //String
    console.log(typeof(123));   //Number
    console.log(typeof(true));  //Boolean
    console.log(typeof());      //NULL
    console.log(typeof(abc));   //Undefined
    
    console.log([] instanceof Array);                   // true
    console.log({} instanceof Object);                  // true
    console.log(function(){} instanceof Function);      // true