JavaScript string length explained: code units, characters, and emoji
JavaScript’s text.length counts UTF-16 code units. The emoji 🙂 has two code units but one code point and one visible cluster. Use Intl.Segmenter for visible character groups and TextEncoder for UTF-8 byte counts.
Count UTF-16 code units with length
length is a property, so read it without parentheses. UTF-16 (16-bit Unicode Transformation Format) represents text with 16-bit code units. Run the code in the console to see that spaces count and an empty string has length zero. Strings are immutable, so assigning to length cannot truncate them. See ECMAScript: String Type.
console.log('abcd'.length); // 4
console.log('\uD55C\uAE00'.length); // 2
console.log(''.length); // 0
console.log('a b'.length); // 3
Count code points in emoji and combining text
Array.from(text).length counts the code points read by the string iterator. It reads the surrogate pair for 🙂 as one. The sequence e\u0301 has two code points, a base letter and a combining accent.
const face = String.fromCodePoint(0x1F642);
console.log(face.length); // 2
console.log(Array.from(face).length); // 1
const accented = 'e\u0301';
console.log(accented.length); // 2
console.log(Array.from(accented).length); // 2
Choose between grapheme clusters and UTF-8 bytes
A grapheme cluster is a unit people generally perceive as one character. With granularity: 'grapheme', Intl.Segmenter counts three clusters in A🙂é. The code uses typeof to test support. Where segmentation is unavailable, it reports that status rather than treating code points as visible characters. See Intl.Segmenter.
const sample = 'A' + String.fromCodePoint(0x1F642) + 'e\u0301';
console.log(sample.length); // 5
console.log(Array.from(sample).length); // 4
if (typeof Intl.Segmenter === 'function') {
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
console.log(Array.from(segmenter.segment(sample)).length); // 3
}
console.log(new TextEncoder().encode(sample).length); // 8
For a storage or transfer limit measured in UTF-8, count bytes with new TextEncoder().encode(text).length. A🙂é occupies eight bytes. State the unit users are limited by and apply the same rule in server validation. See Encoding Standard: TextEncoder.
Complete example: compare four lengths as you type
Save the HTML in UTF-8 as string-length.html and open it. The initial A🙂é has 5 UTF-16 units, 4 code points, 3 grapheme clusters, and 8 UTF-8 bytes.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare four string lengths</title>
</head>
<body>
<label for="text">Enter text</label>
<textarea id="text" rows="3" cols="40">A🙂é</textarea>
<pre id="output" aria-live="polite"></pre>
<script>
const input = document.querySelector('#text');
const output = document.querySelector('#output');
const encoder = new TextEncoder();
const segmenter = typeof Intl.Segmenter === 'function'
? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;
function render() {
const value = input.value;
const points = Array.from(value).length;
const graphemes = segmenter
? Array.from(segmenter.segment(value)).length : "Unavailable in this environment";
output.textContent = `UTF-16 code units: ${value.length}\n` +
`Code points: ${points}\n` +
`Grapheme clusters: ${graphemes}\n` +
`UTF-8 bytes: ${encoder.encode(value).length}`;
}
input.addEventListener('input', render);
render();
</script>
</body>
</html>
Replace it with just 👩💻. The four counts are 5, 3, 1, and 11. Several code points can form a single visible emoji. Clear the input to get zero for all supported counts. If segmenting is unavailable, that line displays a message.




