Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 19 / 31COLLECTIONS AND TEXT

Text is more than characters.

JavaScript strings use UTF-16, which shapes indexing and length.

01 / THE BASICS

Start here.

A string is text written between quotes. JavaScript supports single quotes, double quotes, and backticks. Backticks create a template literal, where ${expression} inserts an evaluated value into text. Strings can be joined with +, and methods such as trim, slice, and includes answer common questions about them. Strings are immutable: a method usually returns new text rather than changing the original string.

Text is more complicated than one character per storage position. JavaScript stores strings as sequences of UTF-16 code units. Many familiar letters fit in one unit, but some symbols need two. That is why a string's length can differ from the number of visible characters. for...of walks Unicode code points more usefully than indexing one code unit at a time, yet a visible symbol can still combine several code points.

For ordinary labels, you can work with strings without thinking about encoding. Encoding matters when you slice by position, count user-visible characters, or compare international text. 'A' and 'a' are different strings; case conversion can help for simple searches, but language rules vary. Normalize and compare text only according to the task's meaning. A display name, a search term, and a password should not automatically use one comparison rule.

02 / THE IDEA

Build the model.

A string is a sequence of UTF-16 code units. Many everyday characters use one unit, while some symbols use two. Therefore string.length does not always equal the number of visible characters a person sees.

for...of iterates Unicode code points, which is often closer to a reader's idea of a character, but a visible grapheme can still contain multiple code points. For user-facing length limits or cursor movement, consider the actual text model you need rather than assuming one index equals one symbol.

03 / UNDER THE SURFACE

How it works.

JavaScript string length counts UTF-16 code units. Many letters occupy one unit, but a symbol outside the basic multilingual plane often uses a pair. Indexing with text[0] returns one code unit, so it may yield only half of such a symbol. for...of and spread walk Unicode code points and keep a surrogate pair together.

A displayed character can still contain more than one code point. For example, a letter plus a combining accent may be drawn as one visible unit. If a user-facing feature needs grapheme clusters, Intl.Segmenter can segment them with granularity: 'grapheme' where supported. For routine comparison, remember that visually identical text can have different Unicode sequences; normalization may be needed at an input boundary.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const symbol = '🚀';
console.log(symbol.length); // 2
console.log([...symbol].length); // 1

First notice. The two counts measure different units of text.

The rocket symbol occupies two UTF-16 code units, so symbol.length is 2. Spread uses the string iterator, which yields the rocket as one code point; the resulting array has length 1. Neither count automatically means 'one character a human sees' for every possible string, because grapheme clusters can contain several code points.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const text = 'A🚀';
console.log(text.length);
console.log([...text].length);

The first result is 3: one unit for A and two for the rocket. The second is 2: the string iterator yields A and the rocket as two code points. For a label or cursor, decide which unit actually matches the user's experience.

06 / KEEP THIS

One thing to remember.

String indices count code units, not necessarily visible characters.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.