JavaScript String codePointAt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.codePointAt(index) returns the full Unicode code point starting at a UTF-16 index. For many emoji that use a surrogate pair, that number can be larger than 65535. Out of range returns undefined.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.codePointAt. Start with this line.

JavaScript
string.codePointAt(index)
CallWhat it does
string.codePointAt(index)Primary signature.
string.codePointAt(...)Call with the arguments your case needs.

Built into JavaScript. Call codePointAt(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
string.codePointAt(index)

Parameters

ParameterDescription
indexRequired. UTF-16 position (not a code-point count)
undefined checkOptional. Needed when missing indexes matter

Return Value

Return ValueDescription
*Non-negative integer, or undefined

JavaScript Examples

Example 1 — ASCII Code Points

For BMP characters, codePointAt matches the familiar ASCII numbers.

JavaScript
const text = "ABC";

console.log(text.codePointAt(0));  // 65
console.log(text.codePointAt(1));  // 66
console.log(text.codePointAt(2));  // 67
Try It Yourself

How It Works

  1. Build the string. "ABC" has three BMP characters at indexes 0–2.
  2. Read each code point. "A" → 65, "B" → 66, "C" → 67.
  3. Same as charCodeAt here. Single-unit characters need no surrogate decoding.
  4. No side effects. The string is unchanged; codePointAt only reads.

Example 2 — Full Emoji Code Point

Start at the emoji’s high surrogate to get the full code point, then rebuild it.

JavaScript
const text = "A😀";

console.log(text.length);                 // 3
console.log(text.codePointAt(0));         // 65
console.log(text.codePointAt(1));         // 128512
console.log(String.fromCodePoint(128512)); // "😀"
console.log(JSON.stringify([...text]));   // ["A","😀"]
Try It Yourself

How It Works

  1. Length counts units. "A😀" is three UTF-16 units, two visible characters.
  2. Index 0 is A. codePointAt(0) → 65.
  3. Index 1 starts the emoji. codePointAt(1) combines both surrogates → 128512.
  4. Rebuild and iterate. fromCodePoint and [...text] work with full characters.

Example 3 — codePointAt() vs charCodeAt()

Same for ASCII; different for emoji — and different out-of-range results.

JavaScript
const text = "A😀";

console.log(text.codePointAt(0));  // 65
console.log(text.charCodeAt(0));   // 65

console.log(text.codePointAt(1));  // 128512
console.log(text.charCodeAt(1));   // 55357  (high surrogate only)

console.log(text.codePointAt(3));  // undefined
console.log(text.charCodeAt(3));   // NaN
Try It Yourself

How It Works

  1. ASCII matches. Both methods return 65 for "A".
  2. Emoji differs. codePointAt(1) is the full emoji; charCodeAt(1) is only the high surrogate.
  3. Missing slots differ. codePointAt → undefined; charCodeAt → NaN.
  4. Rule of thumb. Need the whole character number? Prefer codePointAt.

Browser & Runtime Support

String.prototype.codePointAt() is part of ES2015 and is Baseline Widely available in modern browsers and current JavaScript runtimes.

✓ Baseline · ES2015

String.prototype.codePointAt()

Supported in Chrome, Firefox, Safari, Edge, and modern Node.js / Deno / Bun. Not available in Internet Explorer without a polyfill.

95% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.codePointAt() Excellent

Bottom line: Safe for modern web apps and current Node LTS. For IE11 or very old engines, feature-detect with if (typeof String.prototype.codePointAt === "function") and fall back to a small polyfill — or stick with charCodeAt when you only need BMP characters.

Frequently Asked Questions

codePointAt(index) returns a non-negative integer — the Unicode code point starting at that UTF-16 index. Out of range, it returns undefined.
charCodeAt() always returns one UTF-16 code unit (0–65535). codePointAt() returns the full Unicode code point, which can be larger than 65535 for surrogate pairs (many emoji).
undefined. That differs from charCodeAt() (NaN) and charAt() ("").
Indexes are still UTF-16 code units (same as length and charAt). For a two-unit emoji, index at the high surrogate returns the full code point; the next index lands on the trailing surrogate.
Use String.fromCodePoint(codePoint). Prefer that over fromCharCode() when the value may be above 65535.
Prefer for...of or [...str] — they iterate by code point. A plain for (i = 0; i < str.length; i++) loop visits both halves of a surrogate pair.

Next: Symbol.iterator

Learn how strings expose an iterator that walks by code point with for...of.

Symbol.iterator tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful