I was reading a good book on JavaScript.
It started with:
Boolean type take only two literal values: true and false. These are distinct from numeric values, so true is not equal to 1, and false is not equal to 0.
However, I observed following:
if(1==true)
document.write("oh!!! that's true"); //**this is displayed**
I know, that every type in JavaScript has a Boolean equivalent.
But then, what's the truth?
This question is related to
javascript
type-conversion
Actually every object in javascript resolves to true if it has "a real value" as W3Cschools puts it. That means everything except ""
, NaN
, undefined
, null
or 0
.
Testing a number against a boolean with the ==
operator indeed is a tad weird, since the boolean gets converted into numerical 1 before comparing, which defies a little bit the logic behind the definition.
This gets even more confusing when you do something like this:
var fred = !!3; // will set fred to true _x000D_
var joe = !!0; // will set joe to false_x000D_
alert("fred = "+ fred + ", joe = "+ joe);
_x000D_
not everything in javascript makes a lot of sense ;)
with == you are essentially comparing whether a variable is falsey when comparing to false or truthey when comparing to true. If you use ===, it will compare the exact value of the variables so true will not === 1
From the ECMAScript specification, Section 11.9.3 The Abstract Equality Comparison Algorithm:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
Thus, in, if (1 == true)
, true
gets coerced to a Number
, i.e. Number(true)
, which results in the value of 1
, yielding the final if (1 == 1)
which is true
.
if (0 == false)
is the exact same logic, since Number(false) == 0
.
This doesn't happen when you use the strict equals operator ===
instead:
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
Ah, the dreaded loose comparison operator strikes again. Never use it. Always use strict comparison, === or !== instead.
Bonus fact: 0 == ''
In JavaScript, == is pronounced "Probably Equals".
What I mean by that is that JavaScript will automatically convert the Boolean into an integer and then attempt to compare the two sides.
For real equality, use the === operator.
When compare something with Boolean it works like following
Step 1: Convert boolean
to Number
Number(true) // 1
and Number(false) // 0
Step 2: Compare both sides
boolean == someting
-> Number(boolean) === someting
If compare 1
and 2
with true
you will get the following results
true == 1
-> Number(true) === 1
-> 1 === 1
-> true
And
true == 2
-> Number(true) === 1
-> 1 === 2
-> false
Try the strict equality comparison:
if(1 === true)
document.write("oh!!! that's true"); //**this is not displayed**
The ==
operator does conversion from one type to another, the ===
operator doesn't.
Use ===
to equate the variables instead of ==
.
==
checks if the value of the variables is similar
===
checks if the value of the variables and the type of the variables are similar
Notice how
if(0===false) {
document.write("oh!!! that's true");
}?
and
if(0==false) {
document.write("oh!!! that's true");
}?
give different results
Source: Stackoverflow.com