JavaScript String charCodeAt() Method
The JavaScript str.charCodeAt() method returns a Unicode character set code unit of the character present at the index in the string specified as the argument. The index number ranges from 0 to n-1, where n is the string’s length.
Syntax:
str.charCodeAt(index)
Parameters:
This method accepts a single parameter.
- index: It is the index of the character in the string whose Unicode is to be used. The range of the index is from 0 to length – 1.
Return value:
This method returns the Unicode (ranging between 0 and 65535) of the character whose index is provided to the method as the argument. If the index provided is out of range this method returns NaN
Note: The str.charCodeAt(index) method does not change the value of the original string.
Example 1: This example shows the basic use of the String.prototype.charCodeAt() Method.
function func() {
let str = 'GEEKS';
let value = str.charCodeAt(0);
console.log(value);
}
func();
Output
71
Example 2: In this example, the method charCodeAt() extracts the character from the string at index 4. Since this character is m, therefore this method returns the Unicode sequence as 109.
// JavaScript to illustrate charCodeAt() method
function func() {
let str = 'ephemeral';
// Finding the code of the character at
// given index
let value = str.charCodeAt(4);
console.log(value);
}
func();
Output
109
Example 3: In this example, the method charCodeAt() extracts the character from the string at index 20. Since the index is out of bounds for the string, therefore this method returns the answer as NaN.
// JavaScript to illustrate charCodeAt() method
function func() {
let str = 'ephemeral';
// Finding the code of the character
// at given index
let value = str.charCodeAt(20);
console.log(value);
}
func();
Output
NaN
Supported Browsers:
- Chrome 1
- Edge 12
- Firefox 1
- safari 1
- Opera 4
JavaScript String charCodeAt() Method – FAQs
What is the charCodeAt() method in JavaScript?
The
charCodeAt()
method returns the Unicode value (UTF-16 code unit) of the character at a specified index in a string.
How does charCodeAt() handle non-integer indices?
If the index is not an integer, it is implicitly converted to an integer before the method is applied.
Can charCodeAt() handle Unicode characters outside the Basic Multilingual Plane (BMP)?
charCodeAt() returns the UTF-16 code unit, which may not fully represent characters outside the BMP (characters with code points above U+FFFF). For such characters, you may need to handle surrogate pairs.
How does charCodeAt() differ from codePointAt()?
charCodeAt() returns the UTF-16 code unit at a given index, whereas codePointAt() returns the full Unicode code point, which can handle characters outside the BMP.
Is charCodeAt() case-sensitive?
Yes, charCodeAt() is case-sensitive. It returns different Unicode values for uppercase and lowercase letters.
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.