简体   繁体   English

为什么1&1 === 1返回true而2&2 === 2返回false?

[英]Why 1 & 1 === 1 returns true while 2 & 2 === 2 returns false?

In the console, when I type 1 & 1 it returns 1 , that's basically why 1 & 1 === 1 returns true . 在控制台中,当我输入1 & 1它返回1 ,这就是为什么1 & 1 === 1返回true

For 2 & 2 it returns 2 but 2 & 2 === 2 returns false 对于2 & 2它返回22 & 2 === 2返回false

Why ? 为什么?

 console.log("1 & 1: ", 1 & 1); console.log("1 & 1 === 1: ", 1 & 1 === 1); console.log("2 & 2: ", 2 & 2); console.log("2 & 2 === 2: ", 2 & 2 === 2); console.log("typeof(2): ", typeof 2); console.log("typeof(2 & 2): ", typeof(2 & 2)); 

As @jonrsharpe said, it doesn't return false , it returns 0 . 正如@jonrsharpe所说,它不返回false ,它返回0 It's evaluated as 2 & (2 === 2) . 它被评估为2 & (2 === 2) (2 & 2) === 2 is true . (2 & 2) === 2true

Thanks 谢谢

在javascript中,当你写2和2时,这并不意味着2 && 2而是它将2转换为32位数字并执行AND操作,相同的AND你在二进制数中学习,其中数字不再是2因此(2和2)== = 2不会返回true,因为(1&1)=== 1返回true,因为在二进制操作1和1上也返回1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM