What is charCodeAt() in JS?
Overview
The charCodeAt method is defined in the String class in JS, and returns the UTF-16 code for the specified character inside the string.
Syntax
charCodeAt(index);
Parameters
- The
indexis the number that specifies the position of a character inside the string.indexmust be greater than or equal to0, and less than the length of the string.indexdefaults to the value of0ifindexis not a number.
Return value
- The UTF-16 code, for the character specified by
index, is returned. - If
indexis out of range,NaNis returned.
Example
let str = 'Hello World!';console.log(str.charCodeAt(4));
In the example above, the charCodeAt method is invoked on the str string with the index value 4. The returned character code (111) is the UTF-16 code for the character o.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved