2
'test'.charAt('not an integer');

This code always returns 't', and I'm guessing that when the parameter of charAt() cannot be converted to an integer, it just returns the first character.

But I can't seem to find this behavior documented. Is my assumption correct and could you point me to a resource that documents this behavior?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
hattenn
  • 4,371
  • 9
  • 41
  • 80
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt – sferret Jul 02 '13 at 15:30
  • @sferret, where does it document that behavior on that web page? I can't seem to see it. – hattenn Jul 02 '13 at 15:32
  • 1
    Just go to the question pointed out by epascarello, it's the same exact question with the correct answer. – Reimius Jul 02 '13 at 15:32
  • @Reimius, I have already seen the answer and upvoted it. But I just wanted to see if there was something that I missed on that page where sferret pointed me to. Because I have checked it out before asking this question. – hattenn Jul 02 '13 at 15:34
  • And honestly, I'd be happy if I could find a definitive source on JavaScript documentation. Coming from C# background, I thought MSDN documentation was not good enough, but after seeing JavaScript documentation, I started missing MSDN .NET documentation already. Or maybe I don't know where to look for yet. – hattenn Jul 02 '13 at 15:37
  • I checked the link he posted wondering if it had the ECMA definition (which is what is really relevant to answer your question), but what he/she posted seems to be completely useless for your question. – Reimius Jul 02 '13 at 15:38
  • @Reimius, exactly. That's why I wondered if I was missing something. – hattenn Jul 02 '13 at 15:39
  • Javascript is not like C# with an 'easy' to read documentation library. The best approach for programming in Javascript is typing what you want to do into google and seeing what shows up. There are many resources that are good, but not completely correct (like W3C schools), just remember to take all of them with a grain of salt as they may not be 100% correct or demonstrating the best approach. – Reimius Jul 02 '13 at 15:42
  • sorry, the link does not say that the return from NaN is 0 – sferret Jul 02 '13 at 15:42

1 Answers1

2

Yes, it will return the character at the first position. From the ECMAScript 5 spec

Let position be ToInteger(pos).

and in the spec for ToInteger:

Let number be the result of calling ToNumber on the input argument.

and in the spec for ToNumber:

If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

and back to ToInteger:

If number is NaN, return +0.

Graham
  • 6,484
  • 2
  • 35
  • 39
  • The spec is different than the one in http://stackoverflow.com/questions/9980608/why-javascripts-charat-with-a-string-returns-the-first-letter I think this is a newer spec, am I right? – hattenn Jul 02 '13 at 15:44
  • @hattenn They're both ecma-262 5.1, I think the one I linked to is just the HTML version of the PDF. – Graham Jul 02 '13 at 15:49
  • I knew it worked something like this, but I was confusing the call to ToInteger and ToNumber. They seem like the same word lol. – Reimius Jul 02 '13 at 18:44