Why is javascript charAt () with string returning first letter

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

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

      

Why does the string in the argument seem to evaluate to integer 0 for the charAt () method for strings?

Edit: I knew that using charAt () usually takes an integer and the exercise passes charAt () with a string, and I also knew that the string would most likely be coerced into an integer first, which I was checking that it is NaN ... Thanks to Kendall for suggesting to put this missing bit of information in the right question

Thank!

+3


source to share


8 answers


Because Number('e')

- NaN

, but <any nonempty string>.charAt(NaN)

just returns the first character. This behavior is precisely stated in the specification :

15.5.4.4 String.prototype.charAt (pos)

When calling a method with one pos argument, the following steps are performed: charAt

  • Call CheckObjectCoercible passing the this value as an argument.
  • Let S be the result of calling ToString , giving it the this value as an argument.
  • Let the position be ToInteger (pos).
  • Let the size be the number of characters in S.
  • If position <0 or position β‰₯, return an empty string.
  • Returns a string of length 1 containing one character from S, namely the character at position position, where the first (left-most) character in S is counted at position 0, the next at position 1, and so on.

Step 3 is the crux of the matter. ToInteger

like 'e'

, and 'adfadf'

it is 0

. What for? Again, time to hit the spec :

9.4 ToInteger

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

  • Let the number be the result of calling ToNumber on the input argument.
  • If the number is NaN , return +0 .
  • If the number is +0 , -0 , + ∞, or -∞ , return the number.
  • Return the result of evaluating sign (number) Γ— floor ( abs (number)).

We need to go deeper! What is ToNumber ('e') and what is ToNumber ('adfadf')? If you are surprised that I am going to quote the spec again , I am doing something wrong:



9.3.1 ToNumber Applies to string type

ToNumber applied to strings applies the following grammar to the input string. If the grammar cannot interpret the string as an extension of StringNumericLiteral, then the result is ToNumber NaN .

... I will not list the entire grammar for StringNumericLiteral. Since 'e'

and 'adfadf'

are neither StrDecimalLiteral s nor HexIntegerLiteral s, the ToNumber of both these values ​​is NaN . Finally, we have a transformation: from a line up NaN

to 0

, which returns us to the chain charAt

: position is 0

, therefore, charAt('e')

and charAt('adfadf')

both return the left-most character in the S.

Now, if those lines were instead valid StrNumericLiteral s, like '0xe'

and '0xadfadf'

:

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

      

well that's a different story for a different answer.

+10


source


The JavaScript core string.charAt(idx)

takes an integer argument as the index for which the character is returned.

'abc'.charAt(0); // => 'a'

      

If you give it a non-integer argument, it will apparently try to convert the argument to a number using either a converter Number(arg)

, or perhaps parseInt(arg, 10)

. Both of these functions return NaN

if given strings that are not parsed as an integer, so the function charAt()

should automatically convert NaN

to zero:



Number('e'); // => NaN
parseInt('e', 10); // => NaN
'abc'.charAt(NaN); // => 'a'

      

Perhaps a directly related call charAt()

with no argument returns the first character:

'abc'.charAt(); // => 'a'

      

+4


source


When you call String.charAt (pos) it evaluates the toInteger value for pos first, and since you gave it "e" this evaluates to 0, which will lead to your answer. For more information see below:


According to http://ecma262-5.com/ELS5_HTML.htm#Section_15.5.4.4

String.prototype.charAt (pos)

      

Returns the string containing the character at position pos in String by converting this object to a string.

If pos is a Number, which is an integer, then the result of x.charAt (pos) is the result of x.substring (pos, pos + 1).

When calling the charAt method with one pos argument, the following steps are performed:

  • The CheckObjectCoercible call passes this value as an argument.
  • Let S be the result of calling ToString, giving it that value as its argument.

    3. Let the position be ToInteger (pos).

  • Let the size be the number of characters in S.
  • If position <0 or position β‰₯, return an empty string.

According to http://ecma262-5.com/ELS5_HTML.htm#Section_9.4

9.4 ToInteger

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

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

    2. If the number is NaN, return +0.

  • If the number is +0, -0, + ∞, or -∞, the return number.
  • Returns the result of calculating sign (number) * floor (abs (number)).
+3


source


The charAt () method takes an integer as a parameter. Passing the string will evaluate to NaN, false, 0. Hence, the first character in the string will always be returned.

+2


source


Because it is charAt

trying to transform what you give him into a number. Converting "adfadf" to a number gives you NaN

or Not A Number. The specific behavior for charAt

c NaN

is to return the first character.

+1


source


Both "e" and "adfadf" are strings. Strings, when entered as integer values, always return 0 as the value.

Since "hello" is an array of characters, it will return you the first index, which is "h" (starting at 0).

+1


source


The function charAt

takes an integer as an argument, not a string:

"hello".charAt(0) // 'h'
"hello".charAt(1) // 'e'
"hello".charAt(2) // 'l'

      

And when you give it a string, it basically ends up interpreting it as 0 and returns the first character.

0


source


Friend charAt method is like

string.charAt(index)

      

i.e. you need to pass the index to the method

if you want to do the same use code like

string.charAt(string.indexOf('a'));

      

hope this helps you.

-1


source







All Articles