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 930015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:16:04+00:00 2026-05-15T20:16:04+00:00

just when I thought I understood something about type conversion in JavaScript, I stumbled

  • 0

just when I thought I understood something about type conversion in JavaScript, I stumbled with this:

+[]; // 0
Number([]); // 0

My first thought was that I should get NaN, just like if I try to convert an empty object to number:

+{}; // NaN
Number({}); // NaN

I have been searching about this for a while without any success…

Can somebody explain me why it gets converted to 0 and not to NaN?

Is this behavior standard?

Thanks.

  • 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-15T20:16:05+00:00Added an answer on May 15, 2026 at 8:16 pm

    In a brief, there are two key points:

    • The toString method of an empty array returns an empty string.
    • An empty string coerces to zero using the unary plus operator or the Number constructor called as a function (e.g. +"" === 0;).

    For example, we can use an object that defines a toString method, and returns an empty string to have an equivalent result:

    var obj = { toString: function () { return "";} };
    +obj; //  0
    Number(obj); // 0
    

    This behavior is completely standard.

    Now the long answer:

    Both, the unary plus operator and the Number constructor called as a function internally use the ToNumber abstract operation.

    ToNumber will use two more internal operations, ToPrimitive and [[DefaultValue]].

    When the ToNumber operation is applied to an Object, such the empty array in your example, it calls the ToPrimitive operation, to get a representative primitive value and call ToNumber again using that value [1].

    The ToPrimitive operation receive two arguments, a Value (which is your array object), and a hint type, which in this case is "Number" since we want to make numeric conversion.

    ToPrimitive calls the [[DefaultValue]] internal method, also with a "Number" hint type.

    Now, since the hint type we are using "Number", the [[DefaultValue]] internal method now will try to invoke first the valueOf method on the object.

    Array objects don’t have a specific valueOf method, the method is the one inherited from Object.prototype.valueOf, and this method simply returns a reference to the object itself.

    Since the valueOf method didn’t result in a primitive value, now the toString method is invoked, and it produces an empty string (which is a primitive value), then the ToNumber operation will try to do String-Number conversion and it finally end up with 0 [1].

    But now you might wonder, why an empty string coerces to zero?

    +""; // 0
    

    There is a complete grammar that is used when the ToNumber internal operation is applied to a String type, the StringNumericLiteral production.

    It has some differences between a NumericLiteral, and one of those differences is that:

    A StringNumericLiteral that is empty or contains only white space is converted to +0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just read this question about how to fix something in jQuery 1.2.6 and
I just thought about the non-blocking infrastructure of tornado and event-driven programming. Actually I'm
I probably just haven't thought this through, or perhaps I'm simply unaware of an
I thought that layout is just a widget that keeps more widgets inside. But
I thought I had this figured out but it turns out I'm just deleting
I was just refreshing my JavaScript/Jquery skills and I thought of making a basic
Probably I'm just too dump for googling, but I always thought char arrays get
Something I have never thought too hard about but I am curiuos and want
I've thought long and hard about this and I'm really not sure if it's
I thought that the correct type to use to store the difference between pointers

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.