Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.charCodeAt

Returns the numeric code of a character.

Available since: ES1

Syntax

str.charCodeAt(index)

Parameters

ParameterTypeDescription
indexnumberThe position of the character, counting from zero.

Return value

numberThe UTF-16 code of the character (0 to 65535), or NaN if there is no such position.

Examples

Letter codes:

console.log('A'.charCodeAt(0));
console.log('abc'.charCodeAt(1));

Output:

65
98

Comparing codes shows why capital letters sort before lowercase ones:

console.log('Z'.charCodeAt(0), 'a'.charCodeAt(0));

Output:

90 97

Notes

The reverse conversion is String.fromCharCode(). For emoji use codePointAt(): it returns the full character code.

See also

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.