Table of contents
1.
Introduction
2.
Understanding JavaScript charCodeAt()
2.1.
charCodeAt() Syntax
2.2.
charCodeAt() Parameters
2.3.
charCodeAt() Return Value
3.
JavaScript String charCodeAt() Example
4.
Practical Application of String charCodeAt()
5.
Frequently Asked Questions
5.1.
What is String.fromCharCode()?
5.2.
What is charCode in JavaScript?
5.3.
How to get charCode from a string?
6.
Conclusion
Last Updated: Jan 22, 2025
Easy

JavaScript String charCodeAt()

Author Gunjan Batra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript is a powerful and versatile language. One area where its versatility shines is string manipulation. JavaScript provides several built-in methods to work with strings, one of which is charCodeAt(). 

Javacsript charcodeAt()

In this article, we'll explore how charCodeAt() is used, its purpose, and its importance.

Understanding JavaScript charCodeAt()

The charCodeAt() method is a Javascript function that returns a Unicode value representing the character at the specified index in a string. Unicode is a standard encoding system that is used to represent character from almost all languages. Every character has a unique Unicode value.

charCodeAt() Syntax

The syntax for the charCodeAt() function is quite simple:

string.charCodeAt(index)

Here, string is the string from which we want to retrieve the Unicode value of a character, and index is the position of the character in the string. Remember, JavaScript string indices start from 0.

charCodeAt() Parameters

The charCodeAt() method in JavaScript takes one parameter. index of the character in the string for which you want to retrieve the Unicode value. If no index is provided, the method returns the Unicode value of the first character in the string.

charCodeAt() Return Value

The charCodeAt() method in JavaScript returns a unicode value. The Unicode value of the character at the specified index in the string. If the index is out of range or if the string is empty, NaN (Not a Number) is returned.

JavaScript String charCodeAt() Example

Let's take a look at a simple example:

let myString = "Hello, World!";
let unicodeValue = myString.charCodeAt(0);
console.log(unicodeValue);

In this example, charCodeAt(0) will return 72, the Unicode value for the character 'H'.

Output

Output

It's important to note that if the index is out of range (negative, or greater than or equal to the length of the string), charCodeAt() returns NaN.

Let's take another example.

let str = "JavaScript";
let index = 3; // Index of 'a'
let unicodeValue = str.charCodeAt(index);
console.log(unicodeValue); // Output: 97

In this example, the charCodeAt() method retrieves the Unicode value of the character at index 3 in the string "JavaScript", which is the letter 'a'. The Unicode value of 'a' is 97, so the output will be 97.

Practical Application of String charCodeAt()

Understanding the Unicode value of characters can be very useful. For example, it can be used to sort strings in alphabetical order, or to transform lower-case letters to upper-case, and vice versa.

Here is a simple function that converts a lower-case string to upper-case using charCodeAt():

function toUpperCase(str) {
    let result = '';
    for (let i = 0; i < str.length; i++) {
        let charCode = str.charCodeAt(i);
        if(charCode > 96 && charCode < 123) {
            result += String.fromCharCode(charCode - 32);
        } else {
            result += str[i];
        }
    }
    return result;
}
console.log(toUpperCase('hello world')); 

Outputs

Output

Frequently Asked Questions

What is String.fromCharCode()?

String.fromCharCode() is a JavaScript method that converts Unicode values (character codes) into a string of corresponding characters.

What is charCode in JavaScript?

charCode refers to the numeric Unicode value representing a character. It can be obtained using methods like charCodeAt() or keyCode in events.

How to get charCode from a string?

Use the charCodeAt(index) method on a string to get the Unicode value of the character at a specific index.

console.log("A".charCodeAt(0)); // Output: 65  

Conclusion

The charCodeAt() JavaScript method is a powerful tool for working with strings. It provides a straightforward way to retrieve the Unicode value of characters in a string. Knowing how and when to use it will certainly enhance your string manipulation skills in JavaScript, making your programs more robust and dynamic.

To learn more about JavaScript, we recommend reading the following articles:

Live masterclass