Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ('0' ? 'a' : 'b') behave different than ('0' == true ? 'a' : 'b') [duplicate]

Tags:

javascript

Why is the result of the following two statements different?

('0' ? 'a' : 'b') /* -> 'a' */ ('0' == true ? 'a' : 'b') /* -> 'b' */ 

jsFiddle testcase

Edit:

I should add that I suspect the '0' first statement to be cast to boolean to be compared - which should be exactly the same as " '0' == true " Obviously this is not true.

like image 433
Strayer Avatar asked Sep 21 '11 08:09

Strayer


People also ask

What is the output of 0 == false and 0 === false Why?

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.

Why == is false?

The comparison operator ==, when used with objects, checks whether two objects are the same one. Since there are two {} in the statement {} == {}, two new objects are created separately, and then they are compared. Since they are not the same object, the result is false.

Does the integer 0 == false?

0 and 1 are type 'number' but in a Boolean expression, 0 casts to false and 1 casts to true . Since a Boolean expression can only ever yield a Boolean, any expression that is not expressly true or false is evaluated in terms of truthy and falsy. Zero is the only number that evaluates to falsy.

Is 0 True or false Python?

Python assigns boolean values to values of other types. For numerical types like integers and floating-points, zero values are false and non-zero values are true. For strings, empty strings are false and non-empty strings are true.


1 Answers

First, for completeness:

('0' ? 'a' : 'b')  

is 'a', because '0' is a non-empty string, which always evaluates to true:

String: The result is false if the argument is the empty String (its length is zero); otherwise the result is true.


Now to '0' == true.

Two type conversions will take place here. We can follow this in the specification, section 11.9.3, The Abstract Equality Comparison Algorithm.

The operands are denoted as x and y (x == y).

In our case, x is a string ('0') and y is a Boolean (true). Hence step 7 is executed:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

When booleans are converted to numbers, the following conversion takes place:

Boolean: The result is 1 if the argument is true. The result is +0 if the argument is false.

Now we have

'0' == 1 

which matches the condition in step 5:

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

How strings are converted to numbers is more complex but of course can also be found in the specification.

So the final comparison is

0 == 1 

which is false (step 1. a. vi.)

like image 147
Felix Kling Avatar answered Nov 06 '22 17:11

Felix Kling