string.charAt(i) vs string[i]
Written byPhuoc Nguyen
Category
JavaScript
Created
10 Jun, 2020
There are two ways to access an individual character of a string:
- using the
`charAt[index]`
method - and using bracket notation such as
`'hello'[1]`
In both ways,
`'hello'[1]`
and `'hello'.charAt(1)`
returns the second character `e`
.#Differences
-
The second way is standard of ECMA 5, and is supported in modern browsers. It is not supported in the very old browsers such as IE 6, 7. (I don't think we still need to support these versions of IE).
-
Here is a table listing the expect result of both methods:
Method `index`
is in the range of 0 and`string.length - 1`
Other cases `string.charAt(index)`
character at associate position an empty string `string[index]`
character at associate position `undefined`
We will get different results in some edge cases if you don't pass a proper index (not an integer or out of bounds).'hello'[NaN]; // undefined'hello'.charAt(NaN); // 'h''hello'[undefined]; // undefined'hello'.charAt(undefined); // 'h''hello'[true]; // undefined'hello'.charAt(true); // 'e''hello'['00']; // undefined// return 'h' because it will try to convert `00` to number first'hello'.charAt('00');'hello'[1.5]; // undefined// return 'e' because it will round 1.23 to the number 1'hello'.charAt(1.23);In the case the index is out of bounds:'hello'[100]; // undefined'hello'.charAt(100); // ''
#Good to know
Why does
`'hello'.charAt(true)`
return `e`
?The
`charAt(index)`
method will try to convert the index to the number first. Since `Number(true) === 1`
, `charAt(true)`
will returns the character at the one index position, i.e, the second character.Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn