Read a character or code unit in JavaScript with charAt() and charCodeAt()
charAt(index) returns one UTF-16 code unit as a string. charCodeAt(index) returns its numeric value. Indexes start at zero. An emoji such as 🙂 uses two units, so neither method alone reads it in one call.
Read one code unit as text or a number
A code unit in UTF-16 (16-bit Unicode Transformation Format) is a 16-bit value. Run the snippet in the console. At index 3 of abcde, the results are 'd' and 100. See ECMAScript: charAt() and ECMAScript: charCodeAt().
const text = 'abcde';
console.log(text.charAt(3)); // 'd'
console.log(text.charCodeAt(3)); // 100
console.log(text.length); // 5
Compare out-of-range results
If the converted integer index is below zero or at least length, charAt() returns an empty string and charCodeAt() returns NaN. text[10] returns undefined for the sample. text.at(-1) reads the last code unit.
const text = 'abcde';
console.log(text.charAt(10)); // ''
console.log(text.charCodeAt(10)); // NaN
console.log(text[10]); // undefined
console.log(Number.isNaN(text.charCodeAt(10))); // true
console.log(text.at(-1)); // 'e'
console.log(text.charAt(3.9)); // 'd'
Use Number.isNaN() to test the numeric result. NaN === NaN gives false. These methods truncate fractional indexes toward zero, so charAt(3.9) is the same as charAt(3). Check the input first if you need to require integers.
Distinguish an emoji’s code units from its code point
🙂 has code point U+1F642. Its UTF-16 representation is a surrogate pair, so length is 2. At index 0, codePointAt() combines both units and returns 128578. Index 1 starts at the trailing unit, which gives 56898. See ECMAScript: codePointAt().
const face = String.fromCodePoint(0x1F642);
console.log(face.length); // 2
console.log(face.charAt(0).length); // 1
console.log(face.charCodeAt(0)); // 55357
console.log(face.codePointAt(0)); // 128578
console.log(face.charCodeAt(1)); // 56898
console.log(face.codePointAt(1)); // 56898
console.log([...face].length); // 1
codePointAt() still takes a UTF-16 index. To iterate over code points, for...of reads each surrogate pair together. If you need the text units people see as characters, such as combining sequences or emoji groups, consider Intl.Segmenter with granularity: 'grapheme'.
Complete example: change the string and index
Save the HTML in UTF-8 as string-character-code.html and open it. For the initial string at index 3, the results are "d", 100, and 100. Enter 10 to see "", NaN, and undefined.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Read string positions and codes</title>
</head>
<body>
<label for="text">String</label>
<input id="text" type="text" value="abcde">
<label for="index">UTF-16 index</label>
<input id="index" type="number" min="0" step="1" value="3">
<pre id="output" aria-live="polite"></pre>
<script>
const text = document.querySelector('#text');
const index = document.querySelector('#index');
const output = document.querySelector('#output');
function show() {
const value = text.value;
const position = index.valueAsNumber;
if (!Number.isSafeInteger(position) || position < 0) {
output.textContent = "Enter a nonnegative safe integer index.";
return;
}
const unit = value.charCodeAt(position);
const point = value.codePointAt(position);
output.textContent = `String length (UTF-16 units): ${value.length}\n` +
`charAt(${position}): ${JSON.stringify(value.charAt(position))}\n` +
`charCodeAt(${position}): ${Number.isNaN(unit) ? 'NaN' : unit}\n` +
`codePointAt(${position}): ${point === undefined ? 'undefined' : point}`;
}
text.addEventListener('input', show);
index.addEventListener('input', show);
show();
</script>
</body>
</html>
Replace the string with just 🙂 and compare 0 and 1. With JSON.stringify(), the two charAt() results display as "\ud83d" and "\ude42". The demo field accepts nonnegative safe integers.




