charCodeAt()

ES3+

Returns the UTF-16 code unit at the specified index.

Syntax

string.charCodeAt(index)

Parameters

index number

The index of the character

Return Value

number

A number representing the UTF-16 code unit

Examples

JavaScript
const str = 'ABC';
console.log(str.charCodeAt(0));
console.log(str.charCodeAt(1));
Output:
// 65 66

📌 When to Use

Use charCodeAt() when you need the numeric Unicode value of a character, such as for encryption algorithms, sorting, character classification, or converting characters to their numeric representations.

⚠️ Common Mistakes

Confusing charCodeAt() with codePointAt() - charCodeAt() returns UTF-16 code units, while codePointAt() handles full Unicode code points including emoji and special characters.

Forgetting that charCodeAt() returns NaN for invalid indices, not 0 or undefined.

Not handling surrogate pairs for characters outside the Basic Multilingual Plane (emoji, some Asian characters).

✅ Best Practices

Use codePointAt() instead of charCodeAt() when dealing with emoji or characters outside the BMP (code points > 0xFFFF).

Combine with String.fromCharCode() for character transformations like Caesar cipher or case shifting.

Use charCodeAt() for quick character classification: check if code is between 65-90 (A-Z) or 97-122 (a-z).

⚡ Performance Notes

charCodeAt() is extremely fast as it directly accesses the internal string representation. For processing large amounts of text character-by-character, charCodeAt() can be faster than charAt() followed by comparison, as it avoids creating intermediate string objects.

🌍 Real World Example

Character Encoding and Simple Cryptography

charCodeAt() is essential for implementing character-based algorithms, simple encryption, character classification, and ASCII-based operations.

// Simple Caesar cipher encryption
function caesarCipher(text, shift) {
  let result = '';
  for (let i = 0; i < text.length; i++) {
    const code = text.charCodeAt(i);

    // Uppercase letters (A-Z: 65-90)
    if (code >= 65 && code <= 90) {
      result += String.fromCharCode(((code - 65 + shift) % 26) + 65);
    }
    // Lowercase letters (a-z: 97-122)
    else if (code >= 97 && code <= 122) {
      result += String.fromCharCode(((code - 97 + shift) % 26) + 97);
    }
    else {
      result += text.charAt(i);
    }
  }
  return result;
}

console.log(caesarCipher('Hello World', 3)); // 'Khoor Zruog'

// Check if string contains only ASCII characters
function isAscii(str) {
  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) > 127) {
      return false;
    }
  }
  return true;
}

console.log(isAscii('Hello')); // true
console.log(isAscii('Hello')); // true (Korean characters)

// Count letters, digits, and special characters
function analyzeString(str) {
  let letters = 0, digits = 0, special = 0;

  for (let i = 0; i < str.length; i++) {
    const code = str.charCodeAt(i);
    if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
      letters++;
    } else if (code >= 48 && code <= 57) {
      digits++;
    } else {
      special++;
    }
  }
  return { letters, digits, special };
}

console.log(analyzeString('Hello123!')); // { letters: 5, digits: 3, special: 1 }

Related Methods