extraction
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.
str.codePointAt(position) Parameters
| Parameter | Purpose |
|---|---|
| position | Zero-based index (default 0) |
| returns | Integer code point, or undefined if out of range |
Examples
console.log('A'.codePointAt(0)); Prints 65
console.log('😀'.codePointAt(0)); Prints 128512 (0x1F600)
console.log('😀'.codePointAt(1)); Prints 56832 (trailing surrogate on its own)
console.log('abc'.codePointAt(10)); Prints undefined
Gotcha
position addresses UTF-16 code units, so iterating with i++ still lands inside surrogate pairs — use for...of for code point iteration.
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[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.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.
String.prototype.slice
Extracts a section of the string from beginIndex up to but not including endIndex, returning a new string. Negative indices count from the end.
String.prototype.substring
Returns a new string between indexStart and indexEnd (exclusive). Unlike slice(), negative arguments are treated as 0 and the arguments are swapped when indexStart > indexEnd.