JavaScript String charCodeAt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.charCodeAt(index) returns the UTF-16 code unit (an integer 0–65535) at a zero-based index. Out of range returns NaN. It is read-only and never mutates the string.

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

Syntax

Call string.charCodeAt. Start with this line.

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

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

Library declarations
JavaScript
string.charCodeAt(index)

Parameters

ParameterDescription
indexRequired. Zero-based position (negatives → NaN)
NaN checkOptional. Needed when missing indexes matter

Return Value

Return ValueDescription
*The value returned by string.charCodeAt().

JavaScript Examples

Example 1 — Positive Indexes

Read UTF-16 code units for "A", "B", and "C".

JavaScript
const text = "ABC";

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

How It Works

  1. Build the string. "ABC" has indexes 0, 1, and 2.
  2. Read each code unit. "A" → 65, "B" → 66, "C" → 67 (ASCII / Unicode).
  3. One unit per index. Each BMP character fits in a single UTF-16 code unit.
  4. No side effects. The string is unchanged; charCodeAt only reads.

Example 2 — Out of Range Returns NaN

Missing indexes return NaN — not undefined or "".

JavaScript
const text = "ABC";

console.log(text.charCodeAt(3));              // NaN
console.log(text.charCodeAt(-1));             // NaN
console.log(text.charCodeAt(text.length - 1)); // 67
console.log(String.fromCharCode(65, 66, 67)); // "ABC"
Try It Yourself

How It Works

  1. Past the end. Index 3 is outside 0…2 → NaN.
  2. Negatives fail. charCodeAt(-1) is also out of range → NaN.
  3. Last the classic way. length - 1 reads the final code unit → 67.
  4. Round-trip. fromCharCode turns codes back into "ABC".

Example 3 — charCodeAt() vs codePointAt()

Emoji use two UTF-16 units; only codePointAt returns the full code point.

JavaScript
const text = "A😀";

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

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

console.log(text.length);          // 3  (A + 2 units for 😀)
Try It Yourself

How It Works

  1. ASCII matches. For "A", both methods return 65.
  2. Emoji splits. charCodeAt(1) returns one surrogate (55357), not the whole emoji.
  3. Full code point. codePointAt(1) returns 128512 for 😀.
  4. Length tip. text.length counts UTF-16 units (3), not visible characters (2).

Browser & Runtime Support

String.prototype.charCodeAt() is Baseline Widely available — supported in every modern browser and legacy environments for decades.

✓ Baseline · Widely available

String.prototype.charCodeAt()

Supported in all evergreen browsers, Internet Explorer, and every current Node.js / Deno / Bun release.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 4+ · Legacy environments
Full support
Opera All 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.charCodeAt() Universal

Bottom line: Safe everywhere, including IE. Use charCodeAt for UTF-16 code units; switch to codePointAt when you need full Unicode code points (emoji and astral characters).

Frequently Asked Questions

charCodeAt(index) returns an integer from 0 to 65535 for the UTF-16 code unit at that zero-based index. If the index is out of range, it returns NaN.
charCodeAt() always returns one UTF-16 code unit (0–65535) and may return a lone surrogate for emoji. codePointAt() returns the full Unicode code point, which can be larger than 65535 for surrogate pairs.
NaN. That differs from charAt(), which returns an empty string "" for a missing index.
No. A negative index is out of range, so charCodeAt(-1) returns NaN. Use charCodeAt(str.length - 1) for the last code unit.
Use String.fromCharCode(code) for a UTF-16 code unit, or String.fromCodePoint(codePoint) when you have a full Unicode code point.
No. Strings are immutable. charCodeAt() only reads a number from the string.

Next: codePointAt()

Learn how to read full Unicode code points — including emoji that span two UTF-16 units.

codePointAt() 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.

12 people found this page helpful