JavaScript-数据类型和判断

本文深入解析JavaScript中的8种数据类型,包括基本类型如String、Number、BigInt等,以及Object类型如Function、Array、Date和RegExp。文章详细介绍了如何使用typeof、instanceof、===和Object.prototype.toString.call等方法进行类型判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(8种)数据类型

数据类型文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

分类:基本类型 primitives、Object类型

  • 基本类型(基本数值、基本数据类型)是一种既非对象也无方法的数据。在 JavaScript 中,共有7种基本类型StringNumberBigIntBooleanNullUndefinedSymbol (ECMAScript 2016新增)。详情查看MDN文档
  • Object类型包括:函数Function:特别的对象(可以执行)、数组Array:特别的对象(数值下标、内部数据有序)以及特殊的正则(RegExp)和日期(Date)。

BigInt类型: 属于基础数值类型,可以存储和操作操作大整数,甚至可以超过数字的安全整数限制,通过在整数末尾附加 n 或调用构造函数来创建的BigInt类型的数据。

判断数据类型

方法:1、typeof 2、instance of 3、=== 4、Object.prototype.toString.call

  • typeof:(除了null、Array、Reg、Date外(这几个使用typeof 返回值都是 ‘object’)其他都能判断)可以判断 undefined、number、string、boolean、bigint、symbol,typeof 也可以判断 object 和 function 类型,不能用来判断区别 null 与 object ,以及 object 和Array 。
  • instanceof:判断对象 Object 的具体类型,A instanceof B 判断A是否是B构造函数的实例,返回值为boolean
  • === :可以判断 null 、undefined
  • Object.prototype.toString.call:可以判断某个对象属于哪种内置类型,toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型,是 toString运行时this指向的对象类型, 返回的类型格式为[object,xxx],用call来强制执行Object的toString方法。

注意:typeof 返回的是数据类型的字符串表达

/*
      * typeof:可以判断 undefined/number/string/boolean/(null除外,typeof null===“object”)typeof也可以判断object和function类型
      * instanceof:判断对象Object的具体类型,A instanceof B 判断A是否是B构造函数的实例,返回值为boolean
      * === :可以判断null/undefined
      */

      //typeof 返回的是数据类型的字符串表达
      var a
      console.log(a,typeof a,typeof a==='undefined',a===undefined) //undefined 'undefined' true true
      console.log(undefined==='undefined') //false

      a=3
      console.log(typeof a==='number') //true

      a='ljy'
      console.log(typeof a==='string') //true

      a=true
      console.log(typeof a==='boolean')//true

      var a=123n;
     console.log(typeof a==='bigint')//true
     
     const symbol1 = Symbol();
     console.log(typeof symbol1==='symbol')//true
    
      a=null
      console.log(typeof a,a===null)//object true(所以typeof不能用来判断null类型)
      //Object类型-------------------------------------
      var b1={
        b2:[123,'ljy',console.log],
        b3:function () {
          console.log('function b3')
        }
      }
      console.log(b1 instanceof Object,b1 instanceof Array) //true false
      console.log(b1.b2 instanceof Object,b1.b2 instanceof Array) //true true
      console.log(b1.b3 instanceof Object,b1.b3 instanceof Function)//true true
      //typeof也可以判断object和function类型,array不可以
      console.log(typeof b1,typeof b1.b2,typeof b1.b3)//object object function
      console.log(typeof b1==='object',typeof b1.b2==='array',typeof b1.b3==='function')//true false true
      console.log(typeof b1.b2[2])//function

使用Object.prototype.toString.call方法来判断数据类型:

Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(“abc”);// "[object String]"
Object.prototype.toString.call(123);// "[object Number]"
Object.prototype.toString.call(true);// "[object Boolean]"
      //*************************函数类型**************************
      Function fn(){
        console.log(“test”);
      }
      Object.prototype.toString.call(fn); // "[object Function]"
      //*************************日期类型**************************
      var date = new Date();
      console.log(Object.prototype.toString.call(date));// "[object Date]"
      //*************************正则表达式************************** 
      var reg = /[hbc]at/gi;
      console.log(Object.prototype.toString.call(reg)); // "[object RegExp]"

判断自定义类型
如判断person是Person类创建的实例,只能用instanceof 操作符来进行判断:

      // 实例:实例对象
      // 类型:类型对象
      function Person(name,age) {//创建构造函数  类型
        this.name=name;
        this.age=age;
      }
      var person=new Person('tony',15);//根据类型创建的实例对象
      console.log(typeof person);//object
      console.log(Object.prototype.toString.call(person));//[object Object]
      console.log(person instanceof Person);//true
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值