The length of a JavaScript string is its length property, which counts UTF-16 code units, and you read single characters with bracket notation (str[0]), at() or charAt(). For plain ASCII text the length equals the number of visible characters, but emoji and some accented letters take two code units, so the count can surprise you.
This guide covers the safe ways to read length and access characters, and when you need to count real characters instead.
length is a read-only property, not a method, so there are no parentheses.
Because strings are immutable, you cannot assign to length to shorten or extend a string. A common use is a guard before you process input:
Strings are zero-indexed. The first character is at index 0 and the last is at length - 1.
Out-of-range bracket access returns undefined and does not throw. Assigning to an index does nothing (or throws in strict mode), because strings cannot be modified in place.
at() was added in ES2022 and accepts negative indexes that count from the end. It is the cleanest way to read the last character.
charAt() is older. It returns an empty string instead of undefined when the index is out of range.
| Method | Out of range | Negative index | Returns |
|---|---|---|---|
s[i] | undefined | undefined | character |
s.at(i) | undefined | counts from the end | character |
s.charAt(i) | "" | "" | character |
s.charCodeAt(i) | NaN | NaN | UTF-16 code unit number |
s.codePointAt(i) | undefined | undefined | full code point number |
JavaScript stores strings as UTF-16. Characters outside the Basic Multilingual Plane, such as most emoji, use two code units called a surrogate pair. length counts code units, not characters.
The same problem happens with a letter followed by a combining accent. The text "é" can be one code point (\u00E9) or two (e plus \u0301).
The string iterator walks code points, so spreading a string or using for...of handles surrogate pairs correctly.
For what a user perceives as one character (a flag, a family emoji, a letter with accents), use Intl.Segmenter, available in current browsers and Node.js.
Use length for quick checks and storage limits, and use Intl.Segmenter when you show a character counter to users.
A classic index loop is fine for ASCII data. For general text prefer for...of.
It is a property. Write str.length, not str.length(); the second form throws a TypeError because a number is not a function.
Use str.at(-1). In older environments use str[str.length - 1].
length counts UTF-16 code units, and emoji outside the Basic Multilingual Plane take two. Use [...str].length for code points or Intl.Segmenter for visible characters.
No. Strings are immutable, so build a new string with slice and concatenation, for example s.slice(0, i) + "x" + s.slice(i + 1).
str.length gives the number of UTF-16 code units, and str[i], at() and charAt() read characters, with at() supporting negative indexes. Out-of-range reads give undefined (or "" for charAt). For emoji and combining marks, count with the string iterator or Intl.Segmenter rather than length.