extraction

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].

str.at(index)

Parameters

Parameter Purpose
index Integer position; negative counts from end (-1 is last)
returns Single-character string or undefined if out of range

Examples

console.log('hello'.at(0));

Prints 'h'

console.log('hello'.at(-1));

Prints 'o'

console.log('hello'.at(10));

Prints undefined

console.log('😀'.at(0));

Prints '\uD83D' (lone surrogate half — at() is code-unit based)

Gotcha

Out-of-range indices return undefined (not ''). Indexes UTF-16 code units, so astral characters like emoji take two positions.

Related methods

← All JS string methods · JS array methods