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 index is the number that specifies the position of a character inside the string. index must be greater than or equal to 0, and less than the length of the string. index defaults to the value of 0 if index is not a number.

Return value

  • The UTF-16 code, for the character specified by index, is returned.
  • If index is out of range, NaN is 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