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
String.prototype.at
Returns the UTF-16 code unit at the given index as a single-character string, supporting negative indices that count from the end. Added in ES2022 as a cleaner alternative to str[str.length - 1].
String.prototype.codePointAt
Returns the full Unicode code point (0–0x10FFFF) starting at the given position, correctly combining surrogate pairs. Added in ES2015 as the Unicode-safe counterpart to charCodeAt.
String.prototype[Symbol.iterator]
Returns an iterator that yields the string's Unicode code points as single-character strings, correctly handling surrogate pairs. This is what powers for...of loops and spread over strings.
String.prototype.localeCompare
Returns a negative number, 0, or a positive number indicating whether the string sorts before, equal to, or after compareString according to the current or supplied locale. Preferred over < and > for user-visible sorting.