! next to a number in a conditional prints true on strict comparison

105 views Asked by At
console.log(false === 0) // false

console.log(false === !1) // true, why does it equate to true using !?

and vice versa for

console.log(true === 1 ) // false

console.log(true === !0) // true

I understand the difference between equality and identity but couldn't understand this behaviour of JS . Please explain ?

7

There are 7 answers

0
Pac0 On

The === requires that both values are the same type, no implicit coercion is performed.

When you compare a boolean to a number with triple ===, you will always get false, since they are not the same type.

But using ! in front of a number (or anything else) will convert it to Boolean first, (!x is same as !Boolean(x)) so the strict comparison can succeed. (actually, using ! on anything will coerce it to a Boolean in JS)


Rules of conversion number-to-boolean conversion :

For the number-to-boolean conversion, 0 and NaN are coerced to false, and any non-zero number is coerced to true. (FYI, null, undefined and the empty string '' are the only other falsy values in JS)

Now, you will have two boolean to compare, and === will return true or false accordingly.

So, to summarize :

in !1, 1 is non-zero, so it gets coerced to true, and !true gives false

so false === !1 is equivalent to false === false, which is a true statement.

You can work out the details for the other comparison.


As an additional resource, if you have time and are interested in learning more, I recommend the very good free ebook "You don't know JS".

1
Adictonator On

Because !1 is treated as a boolean value in JS whereas 0 or 1 are interpreted as number.

You can use JS typeof !1 and typeof 0 to see the difference.

Since, false is also a boolean type, matching it with 0 (number) will result false.

Hope that clears it up.

0
Pinguin895 On

JavaScript will first convert your inverted numer to a boolean:

console.log(false === 0)
-> false

console.log(false === !1)
-> false === false
-> true

and vice versa for

console.log(true === 1 )
-> false

console.log(true === !0)
-> true === true
-> true
0
shreyasminocha On

When you do false === 0, it's comparing the boolean false to the integer 0. In contrast, when you do false === !1, javascript converts the integer 1 to a boolean and the unary ! acts as a "not" operation on what is effectively true. Since 1 is truthy in javascript, !1 converts to false. And vice-versa.

console.log(0) // 0
console.log(!0) // true

console.log(1) // 1
console.log(!1) // false
0
axiac On

!1 is a Boolean (because of the logical NOT operator (!)).
true and false are also Boolean values.
0 is a Number.

Values of different types are never identical (===).

0
bipll On

Do you understand the difference between an integer and a boolean?

console.log(false === 0)  // false
console.log(false === !1) // true
console.log(true === 1 )  // false
console.log(true === !0)  // true
console.log(!1)           // false
console.log(!0)           // true

Identity comparison checks that an object is exactly that, and 1 is not true, neither is 0 false.

0
Amit Chauhan On

Because ! operator convert any value to Boolean type and you are checking against another Boolean that's why false === !1 is giving you true.