59

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?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
Mahesha999
  • 22,693
  • 29
  • 116
  • 189

10 Answers10

79

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.

reinierpost
  • 8,425
  • 1
  • 38
  • 70
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    If numbers such as `1` is indeed converted to `true` for the comparison, then why doesn't `2 == true` also evaluate to true? To prove that `2` is also considered `true` when converted to a Boolean, `!!2 == true` evaluates to true. – user193130 Nov 11 '14 at 17:13
  • 2
    @user193130: Sorry, that was a mistake in the answer, when you compare them, the conversion is from boolean to number. The `true` is converted to `1`, so `1 == true` evaluates to true, while `2 == true` evaluates to false. When you use a value as a condition, the conversion has to be to boolean, because that is the only type that a condition can be. – Guffa Nov 11 '14 at 17:55
  • Ah that makes sense. Just tested `+true === 1` and it evaluates true. – user193130 Nov 11 '14 at 20:52
  • 1
    And I think it is pretty confusing, that -1 equals true in JavaScript! For example if you do if(str.indexOf("something")){ dosomething(); } It does even when "something" is not found in str variable. – McVitas May 31 '15 at 19:25
  • 4
    @McVitas: The `indexOf` method isn't meant to be used that way. Even if `-1` was interpreted as `false`, it would not work if the string is found at the first position. As `indexOf` returns `0` if the string is found at the first position, that would also be interpreted as `false`. You could use `if(str.indexOf("something") + 1)`, but that's pretty obscure, it's better to just look for the special value `-1` using `if(str.indexOf("something") != -1)`. – Guffa May 31 '15 at 20:21
  • if you don't use things like `if (array.length)` instead of `if (array.length > 0)` you are a bad programmer. – Hal50000 Nov 14 '16 at 14:29
44

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.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 3
    Actually, it converts the Integer into a Boolean, and then compares, but right idea. – nonrectangular Oct 25 '13 at 03:51
  • @nonrectangular Just confirmed that it actually converts from a Boolean to an Integer; see comments in the answer above and also [João Silva's explanation](http://stackoverflow.com/a/12236648/2891365) below. – user193130 Nov 11 '14 at 20:55
  • Actually, what the `==` chekcs is called "IsLooselyEqual" – IMm0rtal Apr 28 '23 at 10:48
15

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.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Evandro Silva
  • 1,392
  • 1
  • 14
  • 29
9

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.
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • This answers my question - Will the Boolean gets converted to Number / Number gets converted to Boolean ? The answer is - *Boolean gets converted to Number* – coderpc Jan 29 '18 at 16:49
3

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

Bonus fact: 0 == ''

erikkallen
  • 33,800
  • 13
  • 85
  • 120
3

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
webHasan
  • 461
  • 3
  • 11
2

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

Stefan Nolde
  • 201
  • 3
  • 6
  • "not everything in javascript makes a lot of sense" - this example makes perfect sense if you understand type coercion. – BugBuddy Feb 10 '19 at 17:19
0

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

jacktheripper
  • 13,953
  • 12
  • 57
  • 93
0

In a way, yes it is 1.

Try these examples in Chrome console:

> 2==true
false

> 1==true
true

> true + 1
2

> true + 2
3

> true + true
2

So, the answer is:

  • yes, as soon as you use true in any arithmetical context, it's treated as numeric 1, or as Bruce Lee would say, it becomes one. The same way, false is practically zero. But also,

  • no, if you ask Javascript what it thinks of true, it will say it's a boolean:

> typeof true
'boolean'

It's no surprise in a loosely typed language that sometimes things are not what they are but how you look at them. And if you add true to a string, it will not add 1 but "true" as a string, so at the end of the day, it's by no means equivalent to one. Let me end this with a horrible pun:

true is not a number - but it looks like one

dkellner
  • 8,726
  • 2
  • 49
  • 47
-1

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

Sophie
  • 780
  • 6
  • 18