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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:51:45+00:00 2026-05-26T19:51:45+00:00

According to the MDN JS Doc , the charAt method takes in an integer

  • 0

According to the MDN JS Doc, the charAt method takes in an integer and returns an the character at the index. And

If the index you supply is out of range, JavaScript returns an empty string.

What I found is that it also takes string as an argument and the return value is intriguing.

The example code: http://jsfiddle.net/yangchenyun/4m3ZW/

var s = 'hey, have fun here.'
>>undefined
s.charAt(2);
>>"y" //works correct
s.charAt('2');
>>"y" //This works too
s.charAt('a');
>>"h" //This is intriguing

Does anyone have a clue how this happens?

  • 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-26T19:51:45+00:00Added an answer on May 26, 2026 at 7:51 pm

    The algorithm is described in Section 15.5.4.4 in the specification. There you will see (pos being the parameter passed to charAt):

    (…)
    3. Let position be ToInteger(pos).
    (…)

    ToInteger is described in Section 9.4:

    1. Let number be the result of calling ToNumber on the input argument.
    2. If number is NaN, return +0.
      (…)

    'a' is not a numerical string and hence cannot be converted to a number, so ToNumber will return NaN (see Section 9.3.1) which then results in 0.

    On the other side, if you pass a valid numerical string, such as '2', ToNumber will convert it to the corresponding number, 2.


    Bottom line:

    s.charAt('a') is the same as s.charAt(0), because 'a' cannot be converted to an integer.

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

Sidebar

Related Questions

According to MDN documentation for JSON.stringify : Properties of non-array objects are not guaranteed
According to what I've heard, integer values that are not the processor's word size
According to JavaScript Patterns book (p. 79), this should work: var ob = {
According to the Ruby Set class's documentation, == Returns true if two sets are
according to the twitter api documentation http://dev.twitter.com/doc/get/statuses/user_timeline accessing the current logged in users timeline
According to the String.prototype.replace() page on MDN , I should be able to easily
According to docs numpy's default behaviour is to index arrays first by rows then
So according to MDN (it makes sense) the AnimationEvent has the bubble argument but
I need help with this two elements properties. According MDN element.scrollHeight this a height
According to this discussion , the iphone agreement says that it doesn't allow loading

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.