Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain why '+[] == 0' output 'true' in Javascript? [duplicate]

Tags:

javascript

Explain why +[] == 0 give output 'true' in Javascript?

Please, check example.

+[] == 0 ? alert(true) : alert(false);

And also check. '1+[+[]]' give output '10'

like image 444
Hardik Vadodariya Avatar asked Mar 19 '16 11:03

Hardik Vadodariya


People also ask

Why [] == [] is false in JavaScript?

Because [] creates a new array, so you are comparing one array object with another array object. It's not the contents of the arrays that is compared, the object references are compared. They are not equal because it's not the same object instance.

Why do false == [] and false == ![] Both return true?

Types of x and y are checked when x == y is to be checked. If no rule matches, a false is returned. For [] == true , rule 7 matches, so a result of [] == ToNumber(true) is returned i.e. false is returned. Reason you're getting the same result for ![]

What is result of True == 1 in JavaScript?

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.

Does 0 evaluate to true in JavaScript?

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .


2 Answers

It will be evaluated like below,

1 : +[] == 0 --> +"" == 0

Operator + is having highest priority than == so it will be evaluated first. So during the conversion of an array to number, ToPrimitive() function will be called by passing it as an argument. Since [] is an object, it will return "" string

2 : +"" == 0 --> 0 == 0

An empty string will be converted to 0. And a non-empty string would be converted to NaN as we all know.

3 : 0 == 0 --> true

And finally as per abstract equality comparison algorithm, when both operands of same type getting compared, no further evaluation will happen, it will directly checks for its equality and return the result.


And in your second case 1+[+[]], evaluation will happen like,

1 : 1+[+[]] - ( +[] will be converted to primitive first since [] it is an object)

2 : 1+[+""] ( toPrimitive([]) will be "" )

3 : 1+[0] ( 0 will be yielded when you convert an empty string to number )

4 : 1+"0" ( toPrimitive([0]) will be "0" )

5 : "10"

like image 200
Rajaprabhu Aravindasamy Avatar answered Oct 09 '22 18:10

Rajaprabhu Aravindasamy


JavaScript evaluates +[] == 0 this way:

  1. + []: + operator tries to convert the operand [] to a primitive value.
  2. + []: [] is transformed to a string using toString() method, which is an alias for [].join(','). The result is an empty string ''
  3. + '': the empty string is transformed to a number: Number('')-> 0
  4. + 0 becomes 0
  5. Finally the comparison is evaluated: +[] == 0 -> 0 == 0 -> true

The '1+[+[]]' evaluation:

  1. 1 + [ +[] ] (Transform [] into a primitive: '')
  2. 1 + [ + '' ] (Transform '' into a number: 0)
  3. 1 + [ + 0 ] (+ 0 is 0)
  4. 1 + [ 0 ] (Addition operator forces the transform of [0] into a primitive value: [0].toString() -> [0].join(',') -> '0')
  5. 1 + '0' (Because the second operand '0' is a string, transform the first number 1 to a string too: '1')
  6. '1' + '0' (Simple strings concatenation)
  7. '10'

Read also this article about the addition operator.

like image 5
Dmitri Pavlutin Avatar answered Oct 09 '22 19:10

Dmitri Pavlutin