Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
yusijs12508yjavascript you fickle bitch. Did you catch the if(undefined == null) stuff a while ago? was hilariois
-
tahnik389918yAh, this is why I love JavaScript.
In JS when there is a binary operator, operands are first converted to primitive types before operating.
Primitive value for an array is ' ' and for and object is '[object Object]'.
So in your first example you are adding (' ' + '[object Object]') which gives you '[object Object]'
In you second example however you have an object before an operator. So JS ignore the {} part thinking it is an empty code block.
so all that remains is + [ ] which is 0. -
tahnik389918yyou might be thinking why + [ ] is 0.
In JS if you have operators like + before anything and after nothing, it is considered as a unary operator.
So + '1' will be just 1.
+ '2' will be just 2.
Similarly (+ [ ]) = (+ ' ') = (+ 0) = 0 -
watzon46248yAlso in somewhat older JavaScript {} + {} = NaN. This happens because the first {} is evaluated as an empty statement which leaves +{}. The + sign attempts to convert the object into a number which returns NaN
Makes sense
undefined
javascript