JS Built-ins
StringString.prototype.lengthadvanced

String.prototype.length โ€” Unicode surrogate pairs

๐Ÿ“– Usage Example
'hello'.length    // โ†’ 5
''.length         // โ†’ 0
'๐Ÿ˜€'.length       // โ†’ 2

The length property returns the number of UTF-16 code units in the string.

String.prototype.length โ€” Surrogate Pairs

Characters outside the Basic Multilingual Plane (code points > U+FFFF) require two UTF-16 code units, so their .length is 2.

Challenge: Implement surrogateLength() that returns the length of '๐ ฎท' (U+20BB7).

surrogateLength() // โ†’ 2
๐Ÿ’ก Hints (2)
  • โ€ข Characters with code points above U+FFFF require a surrogate pair (2 code units).
  • โ€ข `"๐ ฎท".length === 2` even though it is a single Unicode character.