20
Comments
  • 1
    Too bad Node can't fix JavaScript :(
  • 0
    javascript you fickle bitch. Did you catch the if(undefined == null) stuff a while ago? was hilariois
  • 3
  • 4
    Ah, 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.
  • 1
    you 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
  • 0
    Also 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
Add Comment