es中的 | 和 || 实战(区别实例)

| 是位运算

参加运算的两个数据按照二进制位进行“与”运算,如果两个相应的二进制位都为1,那该位的结果值就是1,否则为0,即0&0=0,0&1=0,1&1=1,1&0=0。
比如:3&5并不等于8,应该按位与 00000011&00000001=00000001。
例子:


//(取整数+换算)
console.log(2.2323 | 0)  ==> 2
console.log(2.2323 | 1)  ==> 3
console.log(2.2323 | 2)  ==> 4
console.log(2.2323 | 3)  ==> 5
//----------我以为很简单的换算方式,没想到噩梦-------------------
console.log(2 | 2) ==> 2
console.log(2 | 3) ==> 3
console.log(2 | 4) ==> 6
console.log(2 | 5) ==> 7
console.log(2 | 6) ==> 6
console.log(2 | 7) ==> 7
console.log(2 | 8) ==> 10
console.log(2 | 9) ==> 11
console.log(2 | 10) ==> 10
console.log(2 | 11) ==> 11
console.log(2 | 12) ==> 14
console.log(2 | 13) ==> 15
console.log(2 | 14) ==> 14

我疯了  这是怎么换算的啊!!!

|| 还是常用的或(如果左端为true,右端不会进行运算)

let a = '2222'
console.log(a || 14) ==> 2222
let b = '2222'
console.log(b || 6666) ==> 2222
let c = ''
console.log(c || 8888) ==> 88888
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

50W程序员都在看

qiugan

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

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

打赏作者

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

抵扣说明:

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

余额充值