Reputation: 791
In the console, when I type 1 & 1
it returns 1
, that's basically why 1 & 1 === 1
returns true
.
For 2 & 2
it returns 2
but 2 & 2 === 2
returns 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));
Upvotes: 3
Views: 385
Reputation: 2150
in javascript when you write 2 & 2 this doesnt mean 2 && 2 instead it convert 2 into 32 bits numbers and perform AND operation, the same AND you learned in binary numbers where the numbers wont be 2 again therefore (2 & 2) === 2 wont return true, as (1 & 1) === 1 returns true because on binary operation 1 AND 1 returns 1 as well
Upvotes: 0
Reputation: 791
As @jonrsharpe said, it doesn't return false
, it returns 0
. It's evaluated as 2 & (2 === 2)
. (2 & 2) === 2
is true
.
Thanks
Upvotes: 1