Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7167727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:38:07+00:00 2026-05-28T14:38:07+00:00

I’ve seen this pretty funny screencast yesterday about some oddities in languages like Ruby

  • 0

I’ve seen this pretty funny screencast yesterday about some oddities in languages like Ruby and Javascript and the guy shows that:

[] + [] -> ""  // returns empty string

not very obvious…

I’ve decided to go to the ECMAscript language specification to get more info. I’ve started with the + operator implementation (p.75) which says:

11.6.1 The Addition operator ( + )

The addition operator either performs string concatenation or numeric addition.
The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows:
1. Let lref be the result of evaluating AdditiveExpression.
2. Let lval be GetValue(lref).
3. Let rref be the result of evaluating MultiplicativeExpression.
4. Let rval be GetValue(rref) .
5. Let lprim be ToPrimitive(lval).
6. Let rprim be ToPrimitive(rval).
7. If Type(lprim) is String or Type(rprim) is String, then
a. Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim). See the Note below 11.6.3.

NOTE1 No hint is provided in the calls to ToPrimitive in steps 5 and 6. All native ECMAScript objects except Date objects handle the absence of a hint as if the hint Number were given; Date objects handle the absence of a hint as if the hint String were given. Host objects may handle the absence of a hint in some other manner.

NOTE 2 Step 7 differs from step 3 of the comparison algorithm for the relational operators (11.8.5), by using the logical-or operation instead of the logical-and operation.

My guess was that the point 7 was reached in some way through different evaluations/conversions as described before but I cannot figure out what is really happenning…

Can someone provide a much straightforward explanation for this ?


I’m willing to understand all this a bit better in order to try to answer why:

[] + {} -> [object Object]
{} + [] -> 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:38:08+00:00Added an answer on May 28, 2026 at 2:38 pm
    1. Let lprim be ToPrimitive(lval).
    2. Let rprim be ToPrimitive(rval).

    Since no hint is provided to ToPrimitive:

    When the [[DefaultValue]] internal method of O is called with no
    hint, then it behaves as if the hint were Number, unless O is a Date
    object (see 15.9.6), in which case it behaves as if the hint were
    String.

    So it’s same as calling with a Number hint:

    When the [[DefaultValue]] internal method of O is called with hint Number, the following steps are taken:
    1. Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf".
    2. If IsCallable(valueOf) is true then, a. Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and  an empty argument list. b. If val is a primitive value, return val.
    3. Let toString be the result of calling the [[Get]] internal method of object O with argument "toString".
    4. If IsCallable(toString) is true then, a. Let str be the result of calling the [[Call]] internal method of toString, with O as the this value and  an empty argument list. b. If str is a primitive value, return str.
    5. Throw a TypeError exception.
    

    Since the return value of [].valueOf() is not a primitive, it goes to [].toString() which returns "".

    This same applies for object objects, except the .toString of an object returns [object Object] while .toString of an array returns .join(",")


    {} + []

    The {} is not treated as an object here but as an empty block. So the code that runs is:

    {}
    +[]; //Same as +[] ( Number([]) ) which is 0
    

    To resolve the ambiguity and get normal result from {} + [] use

    ({}) + [] or ({} + [])

    Funny code:

    {} ! [] //false
    ({}) ! [] //SyntaxError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.