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

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 show him that in PHP, the following sentences would return TRUE when the comparison operator == was used.

So:

var_dump(1 == true); // TRUE

var_dump(2 == true); // TRUE

However, when I applied this to the console to exemplify with javascript, the result was this:

test1 = 1 == true;

test2 = 2 == true;

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

Update

When we instantiate Boolean with these numbers, the results are different:

new Boolean(1); // true
new Boolean(2); // true
new Boolean(0); // false
    
asked by anonymous 28.07.2015 / 17:01

3 answers

14

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

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

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

On the other hand there are languages that can not even do this comparison since the data types are different.

JavaScript is like this. But let's remember that it's also a language that tries to do automatic conversions. And the conversion of true is done to 1. Just as the conversion of false is made to 0.

So the comparison is made to the numeric value and obviously 2 is different from 1. If true is equal to 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 it avoids this kind of problem. In this case the comparison of different types will always be considered false . The == operator should only be used to do funny things. Even if you do not really want to take the type into account, you should do the type conversion explicitly, even though the language would automatically do it. Being explicit is always more interesting in such cases.

    
28.07.2015 / 17:09
14

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

1 == true

In fact you are doing:

1 == 1

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

2 == 1

What, of course, will return% with%. More details can be found in this SOEN response .

    
28.07.2015 / 17:07
2

If I'm not mistaken in Javascript the equality test with two equals ( == ) 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

Even though there are two types of equals sign comparison in javascript

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

I hope I have helped.

Source: link

    
28.07.2015 / 17:11