7

I apologize in advance because this is somehow a silly question, but I just need to know WHY this happens and I didn't find the answer. So, there you go, stack overflow!

In this video (which I can't recommend enough) around 2:00 mark the guys shows that in Javascript:

[] + [] = empty string
[] + {} = object
{} + [] = 0
{} + {} = NaN

Anyone knows why?

3
  • 7
    JavaScript is both fascinating and frightening... +1 Commented Jan 27, 2012 at 19:56
  • 1
    JS has ALL SORTS OF implicit conversions based on the operator. JavaScript guru coming to explain each in 5...4...3.. Commented Jan 27, 2012 at 19:58
  • 1
    [] + {} is the string representation of an object, which is usually something like "[object Object]" jsconsole.com/?typeof%20(%5B%5D%20%2B%20%7B%7D) Commented Jan 27, 2012 at 20:13

1 Answer 1

6

This blog post by Adam Iley covers the reasoning behind most of those edge cases.

(Note: The site is having some database problems. You can use the google cache until they fix things.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.