Is true == 1 and false == 0 in JavaScript?

JavascriptType Conversion

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

It's true that true and false don't represent any numerical values in Javascript.

In some languages (e.g. C, VB), the boolean values are defined as actual numerical values, so they are just different names for 1 and 0 (or -1 and 0).

In some other languages (e.g. Pascal, C#), there is a distinct boolean type that is not numerical. It's possible to convert between boolean values and numerical values, but it doesn't happen automatically.

Javascript falls in the category that has a distinct boolean type, but on the other hand Javascript is quite keen to convert values between different data types.

For example, eventhough a number is not a boolean, you can use a numeric value where a boolean value is expected. Using if (1) {...} works just as well as if (true) {...}.

When comparing values, like in your example, there is a difference between the == operator and the === operator. The == equality operator happily converts between types to find a match, so 1 == true evaluates to true because true is converted to 1. The === type equality operator doesn't do type conversions, so 1 === true evaluates to false because the values are of different types.

Solution 2 - Javascript

>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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

Ah, the dreaded loose comparison operator strikes again. Never use it. Always use strict comparison, === or !== instead.

Bonus fact: 0 == ''

Solution 6 - Javascript

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

Solution 7 - Javascript

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 
    var joe = !!0; // will set joe to false
    alert("fred = "+ fred + ", joe = "+ joe);

not everything in javascript makes a lot of sense ;)

Solution 8 - Javascript

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

Solution 9 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMahesha999View Question on Stackoverflow
Solution 1 - JavascriptGuffaView Answer on Stackoverflow
Solution 2 - JavascriptMadara's GhostView Answer on Stackoverflow
Solution 3 - JavascriptEvandro SilvaView Answer on Stackoverflow
Solution 4 - JavascriptJoão SilvaView Answer on Stackoverflow
Solution 5 - JavascripterikkallenView Answer on Stackoverflow
Solution 6 - JavascriptwebHasanView Answer on Stackoverflow
Solution 7 - JavascriptStefan NoldeView Answer on Stackoverflow
Solution 8 - JavascriptjacktheripperView Answer on Stackoverflow
Solution 9 - JavascriptSophieView Answer on Stackoverflow