JavaScript String codePointAt() Method

Definition and usage

codePointAt() The method returns the Unicode value at the index (position) in the string.

The index of the first position is 0, the second is 1,....

See also:

charCodeAt() method

charAt() method

indexOf() method

lastIndexOf() method

Unicode

For more information about the Unicode character set, please visit our Unicode Reference Manual.

Example

Example 1

Get the code point value (code point) of the first position in the string:

let text = "HELLO WORLD";
let code = text.codePointAt(0);

Try it yourself

Example 2

Get the code point value of the second position:

let text = "HELLO WORLD";
let code = text.codePointAt(1);

Try it yourself

Example 3

Get the code point value of the last position:

let text = "HELLO WORLD";
let code = text.charCodeAt(text.length-1);

Try it yourself

Example 4

Get the code point value of the 15th position:

let text = "HELLO WORLD";
let code = text.charCodeAt(15);

Try it yourself

The difference between charCodeAt() and codePointAt()

charCodeAt() Is UTF-16,codePointAt() Is Unicode.

charCodeAt() Returns a number between 0 and 65535.

Both methods return an integer representing the UTF-16 code of the character, but only codePointAt() Can return a complete value greater than 0xFFFF (65535) of Unicode values.

For more information about the Unicode character set, please visit our Unicode Reference Manual.

Syntax

string.codePointAt(index)

Parameter

Parameter Description
index

Optional. The index (position) in the string.

Default value = 0.

Return value

Type Description
Number The code point value (code point value) at the specified index.
undefined If the index is invalid.

Browser support

codePointAt() Is an ECMAScript6 (ES6) feature.

All modern browsers support ES6 (JavaScript 2015):

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Supported Supported Supported Supported Supported

Internet Explorer 11 (or earlier versions) does not support codePointAt().

Related pages

JavaScript String

JavaScript String Methods

JavaScript String Search