String

String.prototype.charCodeAt

Returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index

Syntax

JavaScript
string.charCodeAt(index)

Parameters

ParameterTypeDescription
indexnumberZero-based index of the character

Return Value

A number representing the UTF-16 code unit value

Examples

Basic Usage
const str = 'ABC';
console.log(str.charCodeAt(0)); // 65
console.log(str.charCodeAt(1)); // 66
Practical Example
const emoji = '😀';
console.log(emoji.charCodeAt(0)); // 55357 (high surrogate)
Advanced Usage
function isUpperCase(char: string) {
  const code = char.charCodeAt(0);
  return code >= 65 && code <= 90;
}
console.log(isUpperCase('A')); // true

Understanding String.prototype.charCodeAt

The String.prototype.charCodeAt method in JavaScript returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.

The method signature is string.charCodeAt(index). It accepts 1 parameter: index. When called, it returns a number representing the utf-16 code unit value. Understanding when and how to use charCodeAt() helps you write more expressive, readable code.

Common use cases for String.prototype.charCodeAt include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-codepointat, string-charat, string-fromcharcode, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for String.prototype.charCodeAt is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More String Methods

Other methods in the String object

Related Tools

More String Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.