charCodeAt JavaScript - String Method
This tutorial will explain to you to get the Unicode value of a character from a given index of a string using the charCodeAt() javascript method.
The Unicode is a number (or code) given to each character by the international standard (ISO). The Unicode value of a character is the number that is assigned to it. The Unicode value of a character is not the same as the ASCII value of a character.
The Unicode number lies between 0 and 65,535. For example, the character 'A' has a Unicode value of 65.
So how will you get a character code or Unicode value of a character from a string?
Using charCodeAt() method.
charCodeAt Method In JavaScript
The charCodeAt() method is used to get the Unicode value of any character from a string.
The method is applied to any string with the index value of the character and it returns an integer between 0 to 65,535.
Syntax:
The syntax of charCodeAt() method is following:
str.charCodeAt(index);
You can use a string variable or direct any string to use the method. Let's see an example:
Example: Using direct string
// charCodeAt JavaScript
console.log("ABC".charCodeAt(0));
console.log("ABC".charCodeAt(1));
console.log("ABC".charCodeAt(2));
Example: Using string variable
const quote = "LEARNING TO CODE";
// unicode of the first character
console.log(quote.charCodeAt(0));
// unicode of the last character
console.log(quote.charCodeAt(quote.length - 1));
Index value in charCodeAt method
The index value passed in the string must lie between 0 and string.length - 1, if the index value is beyond this limit then it returns NaN
.
Example
const quote = "Learning to code";
console.log(quote.charCodeAt(-1)); // return NaN
console.log(quote.charCodeAt(100)); // return NaN
The default value of the index is 0, which means if the index value is not provided or the given index is not a valid number the 0 is used as an index.
Example
const quote = "Learning to code.";
console.log(quote.charCodeAt()); // index not given (default index used = 0)
console.log(quote.charCodeAt("X")); // can't be converted to integer
Getting All Character Code Of String
To get all the character code of a string simply loop through all the characters of the string and use the charCodeAt() method.
Example
const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < alphabets.length; i++) {
console.log(alphabets.charCodeAt(i));
}
Displaying character code at different index in String
Here is an example that displays the character code of a string at a different position.
Example
const quote = "Learning to code.";
// charCodeAt JavaScript
console.log("character code at index 0 is " + quote.charCodeAt(0));
console.log("character code at index 1 is " + quote.charCodeAt(1));
console.log("character code at index 2 is " + quote.charCodeAt(2));
console.log("character code at index 10 is " + quote.charCodeAt(10));
console.log("character code at index 20 is " + quote.charCodeAt(20));
console.log("character code at index 100 is " + quote.charCodeAt(100));
Conclusion
In this short guide, we have learned how to use the charCodeAt() method in JavaScript to get the character code of a string. The returned character code value lies between 0 and 65,535.