Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is charCodeAt() in JS?

Hammad Nasir

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.

RELATED TAGS

javascript

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring