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

标签 javascript binary operator-keyword

在控制台中,当我键入 1 & 1 时,它返回 1,这就是为什么 1 & 1 === 1 返回

对于 2 & 2 它返回 22 & 2 === 2 返回 false

为什么?

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));

最佳答案

正如@jonrsharpe 所说,它不返回false,而是返回0。它被计算为 2 & (2 === 2)(2 & 2) === 2true

谢谢

关于javascript - 为什么 1 & 1 === 1 返回 true 而 2 & 2 === 2 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596325/

相关文章:

c++ - 出现错误 C2679 : binary '+=' : no operator found which takes a right-hand operand of type 'int' 的问题

javascript - 当我添加更多 Material 时,我的内容不会超出一页

javascript - 需要 javascript 将 RADcombobox 的选定索引设置为 0

javascript - 输入文件(图像)显示空数组,我应该在数组中包含数据吗?

c++ - 将字符串(二进制)转换为整数

java - 反/序列化二进制数据

ruby - 在 Ruby 中计算整数的二进制描述中的前导数

c# - 为什么 or 运算符在使用 "not equals"时不能在 if 语句中工作?

python - PEP 的地位和结构意味着什么?

javascript - Java 构建器模式可从 Rhino 中的 JavaScript 访问