Tutorials Tonight's Logo
Follow Us

charCodeAt JavaScript - String Method


This tutorial will explain to you to get the Unicode value of a character from a string and how to use the charCodeAt() method.

The Unicode is a number (or code) given to each character by the international standard from 0 to 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?
charCodeAt() method.

charCodeAt method in JavaScipt

charCodeAt Method In JavaScript

The charCodeAt() method is used to get the Unicode value of any character from a string.

The method is applied on 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:

string.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

console.log("ABC".charCodeAt(0));
console.log("ABC".charCodeAt(1));
console.log("ABC".charCodeAt(2));
▶ Try It

Example: Using string variable

const quote = "Learning to code is learning to create and innovate";
console.log(quote.charCodeAt(0)); // first character
console.log(quote.charCodeAt(quote.length - 1));// last character
▶ Try It

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.

const quote = "Learning to code is learning to create and innovate";
console.log(quote.charCodeAt(-1)); // return NaN
console.log(quote.charCodeAt(100));// return NaN
▶ Try It

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.

const quote = "Learning to code is learning to create and innovate.";
console.log(quote.charCodeAt()); // index not given (default index used = 0)
console.log(quote.charCodeAt("X")); // can't be converted to integer
▶ Try It

Getting All Character Code Of String

To get all character code of a string simply loop through all the characters of the string and use the charCodeAt() method.

const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < alphabets.length; i++) {
  console.log(alphabets.charCodeAt(i));
}
▶ Try It

Displaying character code at different index in String

Here is an example that displays characters code of string at a different position.

const quote = "Learning to code is learning to create and innovate.";
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) + "'.");

Points to remember:

  1. To get the character code at some index position in a string use charCodeAt() method.
  2. The returned character code value lies between 0 and 65,535.
Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.