I’m testin some JavaScript concepts and I’m trying to understand why:
typeof({} + {})
typeof([] + [])
return a ‘string’ yet:
typeof {}
typeof []
return an ‘object’
I’ve tried this in NodeJS, FireFox, Chrome and IE9 all with the same results.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
+operator is defined to either concatenate strings or add numbers. If you try something else, the operands have to be converted to strings or numbers first. It looks like string is preferred here.You can control what
stringornumberis returned by defining functions.toStringand.valueOfon the objects respectively