Explain why +[] == 0
give output 'true' in Javascript?
Please, check example.
+[] == 0 ? alert(true) : alert(false);
And also check.
'1+[+[]]'
give output '10'
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.
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 ![]
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.
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 .
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"
JavaScript evaluates +[] == 0
this way:
+ []
: +
operator tries to convert the operand []
to a primitive value. + []
: []
is transformed to a string using toString()
method, which is an alias for [].join(',')
. The result is an empty string ''
+ ''
: the empty string is transformed to a number: Number('')
-> 0
+ 0
becomes 0
+[] == 0
-> 0 == 0
-> true
The '1+[+[]]'
evaluation:
1 + [ +[] ]
(Transform []
into a primitive: ''
)1 + [ + '' ]
(Transform ''
into a number: 0
)1 + [ + 0 ]
(+ 0
is 0
)1 + [ 0 ]
(Addition operator forces the transform of [0]
into a primitive value: [0].toString()
-> [0].join(',')
-> '0'
)1 + '0'
(Because the second operand '0'
is a string, transform the first number 1
to a string too: '1'
)'1' + '0'
(Simple strings concatenation)'10'
Read also this article about the addition operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With