String.prototype.charCodeAt()

charCodeAt(index)
Returns: number · Added in ves6 · Updated March 14, 2026 · String Methods
javascript es6 string unicode encoding

The charCodeAt() method returns an integer representing the UTF-16 code unit at the specified index in a string. This method is essential when you need to work with the numeric representation of characters rather than the characters themselves.

Syntax

string.charCodeAt(index)
  • index: An integer representing the position of the character to retrieve. If omitted, defaults to 0. If negative or greater than or equal to length, returns NaN.
  • Returns: A number representing the UTF-16 code unit value (between 0 and 65535), or NaN if the index is out of bounds.

TypeScript Signature

interface String {
  charCodeAt(index: number): number;
}

Basic Examples

const text = "Hello";

console.log(text.charCodeAt(0)); // 72  (H)
console.log(text.charCodeAt(1)); // 101 (e)
console.log(text.charCodeAt(4)); // 111 (o)
console.log(text.charCodeAt(5)); // NaN (out of bounds)

charCodeAt() vs codePointAt()

JavaScript strings use UTF-16 encoding. Most characters fit in a single 16-bit code unit (values 0–65535), but characters outside the Basic Multilingual Plane (BMP)—including many emojis—require surrogate pairs (two code units).

  • charCodeAt() returns the individual UTF-16 code unit (0–65535)
  • codePointAt() returns the full Unicode code point, properly handling surrogates
const emoji = "😀";

console.log(emoji.length);         // 2 (two UTF-16 code units)
console.log(emoji.charCodeAt(0)); // 55357 (0xD83D - high surrogate)
console.log(emoji.charCodeAt(1)); // 56832 (0xDE00 - low surrogate)
console.log(emoji.codePointAt(0)); // 128512 (0x1F600 - actual emoji codepoint)

Always use codePointAt() when processing emojis or international text.

Practical Use Case: Character Validation

function containsOnlyLetters(str) {
  for (let i = 0; i < str.length; i++) {
    const code = str.charCodeAt(i);
    // Check if character is A-Z (65-90) or a-z (97-122)
    if (!((code >= 65 && code <= 90) || (code >= 97 && code <= 122))) {
      return false;
    }
  }
  return true;
}

console.log(containsOnlyLetters("Hello")); // true
console.log(containsOnlyLetters("Hello!")); // false

Practical Use Case: Custom Sorting

// Sort strings by character codes (case-sensitive alphabetical)
const words = ["Banana", "apple", "Cherry"];

// Sort by first character code
words.sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0));

console.log(words); // ["Banana", "Cherry", "apple"]
// B(66), C(67), a(97) - uppercase comes before lowercase

Converting Back to Characters

Use String.fromCharCode() to convert numeric code values back to characters:

const codes = [72, 101, 108, 108, 111];
const word = String.fromCharCode(...codes);

console.log(word); // "Hello"

Edge Cases

const text = "test";

console.log(text.charCodeAt());     // 116 (defaults to index 0)
console.log(text.charCodeAt(0));    // 116 (t)
console.log(text.charCodeAt(-1));   // NaN (negative index)
console.log(text.charCodeAt(10));   // NaN (out of bounds)
console.log("".charCodeAt(0));      // NaN (empty string)

Browser Compatibility

BrowserVersion
Chrome1+
Firefox1+
Safari1+
Edge12+
Node.js0.1.0+

Universal support across all JavaScript environments.

See Also