JavaScript Code

JavaScript String charCodeAt()


String charCodeAt()

In JavaScript, String.charCodeAt() method is used to get the Unicode-16 character from the string at given index.

Syntax

str.charCodeAt(n)

where

  • str is a string.
  • n is an integer (position/index).

charCodeAt() method returns the UTF-16 code (unicode) character (as integer) from string str present at the index n.

Examples

1. In the following program, we get unicode character in the string name at index=3.

let name = 'apple';
let output = name.charCodeAt(3);
console.log(output);

2. In the following program, we take the index n such that the it outside the range of string, then charCodeAt() must return NaN (Not a Number).

let name = 'apple';
let output = name.charCodeAt(15);
console.log(output);

Copyright @2022