js中不同数据类型之间的运算、比较的结果

[基础数据类型之间的转换]

在js中有5种基础类型数据:string、number、boolean、null、undefined,其中,常用于计算或者比较的类型是前面三种。
在这里插入图片描述
参考、图片来源

不同类型之间的加法运算

  • 有string, 其他类型转为string
  • 没有string,都转换为数字
 		console.log(5/0);//Infinity
        console.log(-5/0);//-Infinity
        console.log("1"+"2");//"12"
        console.log(1+"2");
        console.log(11+"2");//"112"
        console.log(1+undefined);//1+NaN = NaN
        console.log(1+"undefined");//"1undefined"

不同类型之间的比较的转换

  • 有string,且string不全是数字, 和数字比较,string转为数字NaN,返回的一定是false
  • 有string, 且string不全是数字,其他类型转为string
  • 有string, 且string全是数字,都转为数字
  • 没有string,都转换为数字

== 和 ===

		console.log(NaN == undefined);//false
        console.log(NaN===undefined);//false
        console.log(1=="1");//true
        console.log(1==="1");//false
        console.log("1" == "true");//false
        console.log(1 == "true");//false
        console.log("1" == true);//true 两边都转为数字
        console.log(1 == true);//true

>

    	console.log(1>3);//false
        console.log("11" > "3");//false
        console.log("11" > 3);//true,说明字符串转为了数字
        console.log("a" > "3");//true
        console.log("a" > 3);// NaN > 3 false
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值