JavaScript数据类型的隐式转换

隐式转换

(1)字符串连接: 运算符+表示加法运算,JavaScript将把数字转换为字符串,再进行拼接。

var res = 44 + "55";
console.log(res); // 4455

(2)算术运算符(++ – - * / %) 或者比较运算符(> < >= <= != ==)会把两边转化为数字,进行运算

 console.log(44 - "33");    // 11
 console.log(44 - "33a");  // NaN
 console.log(66 * "2");  // 132
// NaN  ===>66 - Number(undefined) = 66 - NaN = NaN
 console.log(66 - undefined);  
console.log("66 > false ==>", 66 > false);  // 在比较过程中,会把false通过Number(false) ==> 0
console.log("66 == '66' ==>", 66 == '66');  // 在比较过程中,会把'66'通过Number('66') ==> 66
/*
  Number(undefined) = NaN; 
  Number(null) = 0;
  NaN ==> 0   
*/
console.log("undefined <= null ==>", undefined >= null);  // false
console.log(null < 0, null > false, null < true)        // false false true  
console.log(null <= 0, null >= false, null <= true)     // true true true

(3)关系运算符 == 和!=

注意: === 和!== 他们是恒等,他们的类型和值豆都要相等,所以不涉及转化

注意:NaN和任意的值,包括自己都不相等

​ NaN == NaN false

undefined只和自己、null相等, null同理 Number(undefined) == NaN

​ undefined == undefined true

​ 特例:undefined == null true

(4)条件表达式 逻辑!(非) 将原本的true变成false, 原本的false变成true

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海面有风

您的鼓励将是我前进的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值