Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.length — string length in UTF-16 units

A string's length property returns its number of UTF-16 code units, which is not always its number of user-perceived characters. Read it as text.length, without parentheses.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

text.length

This String.length entry describes the length property of a string value: use text.length, not text.length(). The result is a nonnegative integer counting UTF-16 code units. An empty string has length 0. This is part of ECMAScript, not a Web API.

Do not equate this value with the number of Unicode code points, user-perceived characters, or bytes. An emoji represented by a surrogate pair occupies two units. A letter followed by a combining mark can also look like one character despite having length 2.

A particular string's length is immutable; assigning to length does not truncate the text. On a String wrapper object, this own property is non-writable, non-enumerable, and non-configurable.

Watch the notation: the literal expression String.length reads the parameter-list length of the String constructor, not a string's length. To measure text, always put the string value before .length.

Examples

const samples = ["", "abc", "\uD83D\uDE00", "e\u0301"];
for (const text of samples) {
  console.log(text.length);
}

Result

0
3
2
2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources