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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:42:25+00:00 2026-06-01T05:42:25+00:00

I am doing some exercises in my object-oriented javascript book, I notice that this:

  • 0

I am doing some exercises in my object-oriented javascript book, I notice that this:

var a = "hello";
a.charAt('e'); // 'h'
a.charAt('adfadf'); //'h'

Why is the string in the argument seemingly evaluated to the integer 0 for the charAt() method for strings?

Edit: I was aware that the charAt()’s usage usually takes an integer, and the exercise feeds charAt() with a string, and I also was aware that the string is likely then to be coerced into an integer first, which I did verify to be NaN. Thanks Kendall, for suggesting putting this missing bit of information in the question proper

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-06-01T05:42:26+00:00Added an answer on June 1, 2026 at 5:42 am

    Because Number('e') is NaN, and <any nonempty string>.charAt(NaN) just returns the first character. This behavior is exactly what is laid out in the spec:

    15.5.4.4 String.prototype.charAt (pos)

    When the charAt method is called with one argument pos, the following steps are taken:

    1. Call CheckObjectCoercible passing the this value as its argument.
    2. Let S be the result of calling ToString, giving it the this value as its argument.
    3. Let position be ToInteger(pos).
    4. Let size be the number of characters in S.
    5. If position < 0 or position ≥ size, return the empty String.
    6. Return a String of length 1, containing one character from S, namely the character at position position, where the first (leftmost) character in S is considered to be at position 0, the next one at position 1, and so on.

    Step 3 is the crux of the matter. ToInteger of both 'e' and 'adfadf' is 0. Why? Again, time to hit the spec:

    9.4 ToInteger

    The abstract operation ToInteger converts its argument to an integral numeric value. This abstract operation functions as follows:

    1. Let number be the result of calling ToNumber on the input argument.
    2. If number is NaN, return +0.
    3. If number is +0, -0, +∞, or −∞, return number.
    4. Return the result of computing sign(number) × floor(abs(number)).

    We need to go deeper! What is ToNumber(‘e’), and what is ToNumber(‘adfadf’)? If you’re surprised that I’m once again about to quote the spec, I’m doing something wrong:

    9.3.1 ToNumber Applied to the String Type

    ToNumber applied to Strings applies the following grammar to the input String. If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

    …I’m not going to quote the entire grammar for StringNumericLiteral. Because 'e' and 'adfadf' are neither StrDecimalLiteral s nor HexIntegerLiteral s, ToNumber of both of those values is NaN. Finally we have the conversion: from string to NaN to 0, which brings us back up the chain to charAt: position is 0, so charAt('e') and charAt('adfadf') both return the leftmost character in S.

    Now, if those strings were instead valid StrNumericLiteral s, such as '0xe' and '0xadfadf':

    > 'hello'.charAt('0xe')
      ""
    > 'hello'.charAt('0xadfadf')
      ""
    

    well, that’s a different story for a different answer.

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

Sidebar

Related Questions

I've been doing some exercises from a book and I'm wondering if you could
I am doing some exercises from the book Thinking In Java . I have
i'm doing some exercises in my Java book. I'm very new to programming. Therefore,
I'm doing some SOAP exercises based on this example: http://www.vankouteren.eu/blog/2009/03/simple-php-soap-example/ But, I cannot get
I am doing some exercises so that I can get through recursive stuff. One
I am doing some weekend coding exercises. I have a table that contains some
I am doing some exercises and tried to convert a simple class into a
I am doing some exercises in assembly language and I found a question about
Doing some homework here (second assignment, still extremely green...). The object is to read
While doing some JavaScript performance tests I came up with the following piece of

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.