The JavaScript charCodeAt() function returns the Unicode code of the character at a specified index position in a string. Instead of extracting the character at the index position, the charCodeAt() function returns the Unicode value of the character.
Example: We use the charCodeAt() method to extract the Unicode value of the character at the 1st index position. The ASCII value of S is 83, so it returns 83.
let s = 'USA'
console.log(s.charCodeAt(1))
83
JavaScript charCodeAt() syntax
The syntax of the charCodeAt() function is
str.charCodeAt(Index_Position)
Parameters: As you can see from the above, the charCodeAt() method takes one parameter, which is index_position. Here, we must provide the index position of the character in the original string that you want to retrieve. The index value must be between 0 (first character) and string length – 1 (last character).
Return Value: The JavaScript charCodeAt() Function returns an integer value between 0 and 65535. The value depends upon the character at the specified index position. It returns the Unicode value of a character from the string object (str) at the specified Index_Position.
- If we don’t pass any index value, the function will return the Unicode value of the first character in a string.
- If we specify an index position out of range, it returns NaN.
JavaScript charCodeAt example
The set of examples to understand the CharCodeAt Function. For more, please refer to the string functions article.
The following example finds the Unicode value of a character at index position 9, which will be G. Remember, you should count the space as one character. ASCII code of G = 71.
let s = 'Tutorial GateWay';
let c = s.charCodeAt(9);
console.log(c);
71
JavaScript charCodeAt reverse
In the following example, we use the built-in len() function in JavaScript to calculate the string length. Next, we subtract 1 from the string length because the length of the original string is 16, and there is no character at the 16th position. It means the code below returns the Unicode value of the last character in a string.
let s = 'Tutorial GateWay';
let c = s.charCodeAt(s.length - 1);
console.log(c);
121
TIP: To traverse from right to left, use the len() function and subtract the value from it.
charCodeAt function with a default parameter
The charCodeAt() method has a default index position of 0. It means when we use the function without any parameter value (index), it uses 0 as the index and returns the Unicode value of the first character in the string.
let s = 'New Year';
console.log(s.charCodeAt());
78
Can the JavaScript charCodeAt() function work on international text?
Yes. As the charCodeAt() function returns the Unicode value between 0 and 65535, we can use the international text. The code below extracts the Unicode value of the Chinese character.
let s = '用中文是';
let c = s.charCodeAt(s.length - 1);
console.log(c);
26159
How to get the Unicode value of the first character?
The index position starts from 0; therefore, we must use the charCodeAt() function with index position 0. The other way is using the charCodeAt() function without any parameters.
let s = 'Hello';
console.log(s.charCodeAt(0));
72
JavaScript charCodeAt with decimal values
As we mentioned earlier, the charCodeAt() method accepts an integer value as the index position. However, when we pass a floating-point or decimal number, the charCodeAt() truncates the values.
In the following example, both 4.2 and 4.9 are truncated to 4 and extract the Unicode value of the characters at the 4th position (e).
let s = 'United States';
console.log(s.charCodeAt(4.2));
console.log(s.charCodeAt(4.9));
101
101
What happens when we use an index out of range?
In the following example, we use the charCodeAt() function to extract the character at the 25th index position. As there are 9 characters in the original string and we are trying to extract the 26th character, it returns NaN.
let s = 'New World';
console.log(s.charCodeAt(25));
NaN
NOTE: If you use the negative index numbers, the charCodeAt() returns NaN. For instance, console.log(s.charCodeAt(-2));