JavaScript String

JavaScript string length, code points, split, replace, and replaceAll, including UTF-16 surrogate pairs.

モジュールページの本文は現在英語です。

String is an ECMAScript built-in. A JavaScript string is a sequence of UTF-16 code units, not a sequence of Unicode code points and not a Node.js Buffer.

Most string methods return a new string. They do not change the original value.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
length, spread, and split("")Choose a code-point iteration method when you need character counts or when you split user-facing text. Do not use split("") for that job.
String.prototype.replace and replaceAllReplace one match, or every match, of a string or regular expression.
String.prototype.split and Array.prototype.joinSplit a string into an array of substrings, or join an array into a string. These are often used when comparing line-oriented or comma-separated text.

UTF-16 length and code points

string.length counts UTF-16 code units. Characters outside the Basic Multilingual Plane, including many emoji, use a surrogate pair and therefore have length 2.

Spreading a string, or calling Array.from(), iterates by code point. string.split("") splits by code unit and can tear a surrogate pair apart.

length, spread, and split("")

string.length
[...string]
Array.from(string)
string.split("")

Choose a code-point iteration method when you need character counts or when you split user-facing text. Do not use split("") for that job.

  • codePointAt(0) on a string that starts with 👍 returns 0x1f44d, the full code point.
  • JSON.stringify shows isolated surrogates as � and � escapes, which is useful in tests.
javascript
const text = "👍";
console.log(text.length);
console.log([...text].length);
console.log(JSON.stringify(text.split("")));
console.log(JSON.stringify(Array.from(text)));

Expected output

2
1
["\ud83d","\udc4d"]
["👍"]

MDN documentation →

replace, split, and trim

replace() with a string pattern replaces only the first match. replaceAll() replaces every match. Both return a new string.

split() with a regular expression is a common way to tokenize text on whitespace. trim() removes whitespace from both ends, including tabs.

String.prototype.replace and replaceAll

string.replace(pattern, replacement)
string.replaceAll(pattern, replacement)

Replace one match, or every match, of a string or regular expression.

ParameterDescription
patternA string or a regular expression. replaceAll() throws TypeError if the pattern is a regex without the global flag.
replacementA string or a function that returns the replacement text.

Returns: A new string.

Throws

  • TypeError — Thrown by replaceAll() when the pattern is a non-global regular expression.
javascript
console.log("aa-aa".replace("a", "b"));
console.log("aa-aa".replaceAll("a", "b"));
console.log("  apple\t".trim());

Expected output

ba-aa
bb-bb
apple

MDN documentation →

String.prototype.split and Array.prototype.join

string.split(separator, limit)
array.join(separator)

Split a string into an array of substrings, or join an array into a string. These are often used when comparing line-oriented or comma-separated text.

javascript
console.log(JSON.stringify("a  b\tc".split(/\s+/)));
console.log(["apple", "pear"].join(","));

Expected output

["a","b","c"]
apple,pear

MDN documentation →