inspection

String.prototype.length

Read-only property giving the number of UTF-16 code units in the string. Not the number of visible characters — emoji and combining marks can inflate the count.

str.length

Parameters

Parameter Purpose
(property) Accessed as a property, not called
returns Non-negative integer count of UTF-16 code units

Examples

console.log('hello'.length);

Prints 5

console.log(''.length);

Prints 0

console.log('😀'.length);

Prints 2 (surrogate pair)

console.log([...'😀'].length);

Prints 1 (spread iterates code points)

Gotcha

Counts UTF-16 code units, not user-perceived characters — for accurate emoji/grapheme counts use [...str].length or Intl.Segmenter. Assignments are silently ignored (strings are immutable).

Related methods

← All JS string methods · JS array methods