Why (1 == true) is "true" and (2 == true) is "false"?

Asked

Viewed 1,869 times

12

I was explaining to a friend of mine the difference between == and === in PHP and wanted to demonstrate this through the javascript console.

I was going to demonstrate to him that in PHP, the sentences below would return TRUE when used the comparison operator ==.

Thus:

var_dump(1 == true); // TRUE

var_dump(2 == true); // TRUE

However, when applying this in the console to exemplify with javascript, the result was this:

test1 = 1 == true;

test2 = 2 == true;

console.log(test1); // true
console.log(test2); // false

Updating

When we make the instance of Boolean with these numbers, the results are different:

new Boolean(1); // true
new Boolean(2); // true
new Boolean(0); // false

3 answers

15

What happens is this, when the true is replaced by a numerical value, it assumes the value of 1, so when you do:

1 == true

In fact you’re doing:

1 == 1

What will return true, in the second case, what will happen will be:

2 == 1

Which, of course, will return false. More details can be found in that SOEN response.

14


It is a decision of each language to determine which values are considered true or false and the programmer’s obligation of each of them to know what is the standard adopted by the language you are using.

The most common is that languages decide that false are the "neutral" values, such as zero, and true is any value other than neutral.

It should be noted that some languages give numerical values to true (1) and to false (0), but this is not the case for JS.

On the other hand, there are languages that cannot even be compared because the data types are different. Truly strong typing languages do not have this problem, false is false and true is true, there is no confusion. In weak typing languages it is possible to make some types if they are false or true.

Javascript is like that. But let’s remember that it is also a language that tries to make automatic conversions. And the conversion of true is made for 1. As well as the conversion of false is made for 0.

So in this case, the comparison is made to the numerical value and obviously 2 is different from 1. If true vale 1 the comparison would look like this:

2 == 1

Remembering that the ideal is always to make the comparison used the operator ===, so the type is taken into account and avoids this kind of problem. In this case the comparison of different types will always be considered false. The operator == It should just be used to do funny things. Even if you really don’t want to take into account the type, you should make the type conversion explicitly, even if the language would do the desired automatically. Being explicit is always more interesting in cases like this.

The ideal is neither to have weak typing, but even if it has only a type coercion precedence in each context for what the programmer expects would already make the language much better.

2

If I’m not mistaken in Javascript the equality test with two equal (==) it converts the comparison values to the nearest level.

Soon 1 == true, it converts true to 1 Soon 0 == false, it converts false to 0

Logo 2 == true or 2 == 1 is false

Still there are two types of comparison with equal sign in javascript

== (x == y)
=== (x == y and the type of x is the same kind of y)

I hope I’ve helped.

Source: http://www.w3schools.com/js/js_comparisons.asp

  • 2

    What actually happens is that true is converted to 1 and false to 0. 2 == true, correct to say 2 == 1. See previous answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.