js中的数据类型以及 == 和 === 和 Object.is() 的区别


一、JS的数据类型

1、基本数据类型

Number String Boolean Underfined Null

2、引用数据类型

Object

3、ES6新增的类型

Symbol

4、谷歌加的

bigInt

二、测试数据类型

1.先简单粗暴的看一下上面写的这几个数据类型是不是真的

    console.log("typeof (Number): " + typeof (Number));
    console.log("typeof (String): " + typeof (String));
    console.log("typeof (Boolean): " + typeof (Boolean));
    console.log("typeof (undefined): " + typeof (undefined));
    console.log("typeof (null): " + typeof (null));
    console.log("typeof (Object): " + typeof (Object));
    console.log("typeof (Symbol): " + typeof (Symbol));
    console.log("typeof (BigInt): " + typeof (BigInt));

结果是这样的,不大对劲,就一个undefined打印出了我想要的结果

换个方式再来试一下,正规点的

    console.log("typeof (1): " + typeof (1));
    console.log("typeof ('jack'): " + typeof ('jack'));
    console.log("typeof (true): " + typeof (true));
    console.log("typeof (undefined): " + typeof (undefined));
    console.log("typeof (null): " + typeof (null));
    console.log("typeof ({}): " + typeof ({}));
    console.log("typeof (Symbol('id')): " + typeof (Symbol('id')));
    console.log("typeof (BigInt('9007199254740995'): " + typeof (BigInt("9007199254740995")));

这次结果不错,基本上全是我想要的了,就一个null不大对头

2.null到底是什么

那就把null拿出来多试几次,看看有什么特别的地方

    console.log("typeof (null): " + typeof (null));
    console.log("typeof (Null): " + typeof (Null));
    console.log("typeof (''): " + typeof (''));
    console.log("typeof ('null'): " + typeof ('null'));
    //console.log("typeof ( ): " + typeof ( ));
    //console.log("typeof (): " + typeof ());

最后一行代码报错了,只能注释掉,倒数第二行放了个空格,也报错,不管怎么样,没一个数据是null类型的,传说中的null是不是不存在啊

百度搜索了下,大致有下面几个结果

1.js的数据是由标记位和数值来表示的,而object和null的标记位都是000,所以就被判断成了object,算是一个bug

2.null表示一个空指针对象,一个指针不知道往哪里指的对象,还是对象

3.null不是一个空引用, 而是一个原始值,它“期望”引用一个对象

不管哪种说法对,都不影响我们敲代码,所以这几种都当故事来看了

刚才搜索的时候又找到了一种判断数据类型的方法,先拿来再测试一下null

    console.log(Number(null));
    console.log(typeof null);
    console.log(Object.prototype.toString.call(null));//刚找到的方法

竟然判断出null这个数据类型了,这就有意思了

又知道了一个新方法,那就把能想到的全都再敲一遍

    console.log("111:"+ Object.prototype.toString.call(111));
    console.log("jack':"+Object.prototype.toString.call('jack'));
    console.log("true:"+Object.prototype.toString.call(true));
    console.log("undefined:"+Object.prototype.toString.call(undefined));
    console.log("null:"+Object.prototype.toString.call(null));
    console.log("{}:"+Object.prototype.toString.call({}));
    console.log("[]:"+Object.prototype.toString.call([]));
    console.log("Symbol('id'):"+Object.prototype.toString.call(Symbol('id')));
    console.log("BigInt('9007199254740995'):"+Object.prototype.toString.call(BigInt('9007199254740995')));
    console.log("new Date():"+Object.prototype.toString.call(new Date()));
    console.log("/[1-9][a-z]/:"+Object.prototype.toString.call(/[1-9][a-z]/));
    console.log("function () {console.log('hello world')}:"+Object.prototype.toString.call(function () {console.log('hello world')}));

把能想到的,嗯,不对,把能抄到的全写了一遍,结果如下

由结果可知,js一共有12种数据类型,当然了,这是不可能的,哪来这么多!

最上面我查到的是八种:Number String Boolean Underfined Null Object Symbol BigInt

这里多出来的四种分别是:Array数组 Date日期 RegExp正则 Function函数,我觉得他们都是Object,最多也就是把正则归类到String,当然还是需要用代码说话

首先判断一个数组

console.log(typeof []);
console.log(Object.prototype.toString.call([]));
console.log([] instanceof Array);
console.log([] instanceof Object);
console.log(Array instanceof Object);

运行结果:[]是Array,而Array是Object,所以[]是Object数据类型

然后Date

let d = new Date();
console.log(typeof d );
console.log(Object.prototype.toString.call(d));
console.log(d instanceof Date);
console.log(d instanceof Object);
console.log(Date instanceof Object);

运行结果如下:说明时间也是Object类型

最后是正则

let d = /[0-9]/;
console.log(typeof d );
console.log(Object.prototype.toString.call(d));
console.log(d instanceof RegExp);
console.log(d instanceof Object);
console.log(RegExp instanceof Object);

运行结果如下,可以看出正则也是Object

最最后还有个function

let d = function () {};
console.log(typeof d );
console.log(Object.prototype.toString.call(d));
console.log(d instanceof Function);
console.log(d instanceof Object);
console.log(Function instanceof Object);

运行结果如下,可以看出function也是Object

测试一圈回来发现,其实只要记住有八种数据类型,null也是数据类型就行了

 

三 .== 和  === 和 Object.is() 的区别

 

== 只比较值,如果类型不同,转换成相同的类型再比较,往String或者Number转

=== 类型和值都相等才相等

Object.is() 和===基本一样,只有下面这个区别

console.log(+0 === -0);//true
console.log(NaN === NaN);//false
console.log(Object.is(+0,-0));//false
console.log(Object.is(NaN,NaN));//true

能把===用好就不错了

== 如果两边数据类型不同,转换规则如下:

1.布尔值===数据,则把布尔值转换为数值

2.字符串===数值,则通过Number()把字符串转为数值

3.如果一边是对象,则调用对象的valueof()方法后再比较

4.null和undefined相等

5.NaN和NaN不相等

6.如果两边都是对象,则比较他们是不是指向同一个对象

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

youngcave2

等待第一笔打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值