String access methods
String access methods are the ways JavaScript lets you read individual characters from a string, or their numeric codes, by position. You use them to grab the first letter of a name for an avatar, check the last character of a file name, or loop through text one character at a time.
Positions start at zero
Every character in a string has an index. The first is at index 0, and the last is at str.length - 1. So in "Delhi", "D" is at 0 and "i" is at 4, while the length is 5. Almost every off-by-one bug with strings comes from forgetting this.
The four main ways to read a character
| Syntax | Returns | Out of range gives |
|---|---|---|
str[i] | The character at i | undefined |
str.charAt(i) | The character at i | "" (empty string) |
str.at(i) | The character at i; negative counts from the end | undefined |
str.charCodeAt(i) | The UTF-16 code unit number at i | NaN |
const city = 'Mumbai';
console.log(city[0]); // "M"
console.log(city.charAt(2)); // "m"
console.log(city.at(-1)); // "i", last character
console.log(city[city.length - 1]); // "i", the older way
console.log(city[10]); // undefined
console.log(city.charAt(10)); // ""
console.log(city.charCodeAt(0)); // 77
const initials = 'Priya Sharma'
.split(' ')
.map((word) => word.charAt(0))
.join(''); // "PS"
Bracket notation is the most common in modern code. at() is the neatest way to read from the end, since str[-1] does not work; it simply looks for a property called "-1" and returns undefined.
Character codes and code points
charCodeAt returns a number such as 65 for "A" or 97 for "a". That is useful for simple tricks like checking whether a character is an uppercase English letter, or for sorting logic. The reverse is String.fromCharCode(65), which gives "A".
Character codes also explain why plain comparisons sort capital letters first: "Z" has the code 90 while "a" has 97, so "Zoo" < "apple" is true. When you need alphabetical order for people, localeCompare is the better tool.
For characters outside the basic range, such as most emoji and some scripts, use codePointAt and String.fromCodePoint, which handle the full Unicode value instead of half of it.
Strings cannot be changed in place
A frequent mistake is trying to replace a character like this: name[0] = 'R'. Nothing happens, and in non-strict code there is not even an error, because strings are immutable. Every string method returns a new string instead. To capitalise a name, build a new one: name.charAt(0).toUpperCase() + name.slice(1).
The emoji surprise
JavaScript strings are stored as UTF-16 code units, and many emoji take two units. So "😀".length is 2, and "😀"[0] returns half of the emoji, which shows as a broken symbol. When text may include emoji, iterate with for...of or spread it with [...str], both of which step through whole code points:
[..."Hi😀"].lengthis 3, matching what a reader sees.for (const ch of str)visits each visible character correctly in most cases.
Which one to use
- Use
str[i]for everyday reads. - Use
str.at(-1)to read from the end. - Use
charAtwhen you prefer an empty string overundefinedfor missing positions. - Use
codePointAtandfor...ofwhen text may contain emoji.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.