JS Built-ins
← String193 / 195
StringString.prototype.lengthintermediate

String.prototype.length β€” emoji length is 2 code units

πŸ“– 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 β€” Emoji

Emoji like πŸ˜€ are encoded as a surrogate pair (two UTF-16 code units), so 'πŸ˜€'.length === 2.

Challenge: Implement emojiLength() that returns the length of the emoji 'πŸ˜€'.

emojiLength() // β†’ 2
πŸ’‘ Hints (2)
  • β€’ Emoji characters outside the Basic Multilingual Plane use two UTF-16 code units (a surrogate pair).
  • β€’ `"πŸ˜€".length === 2` even though it looks like a single character.