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 the charCode?
5.2.
How to get ASCII code in JavaScript?
5.3.
What is the difference between charCodeAt and codePointAt in JavaScript?
5.4.
What does the charCodeAt() method do in JavaScript?
6.
Conclusion
Last Updated: Aug 5, 2024
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 the charCode?

The charCode is the Unicode value of a character in JavaScript. It represents the numeric value of a character based on the Unicode standard.

How to get ASCII code in JavaScript?

To get the ASCII code of a character in JavaScript, you can use the charCodeAt() method. It returns the ASCII value of the character at a specified index in a string.

What is the difference between charCodeAt and codePointAt in JavaScript?

The charCodeAt() method returns the UTF-16 code unit at the specified index, while codePointAt() returns the Unicode code point of the character at the specified index, handling characters outside the Basic Multilingual Plane (BMP) correctly.

What does the charCodeAt() method do in JavaScript?

The charCodeAt() method in JavaScript returns the Unicode value of the character at a specified index in a string. It provides a numeric representation of the character based on the Unicode standard.

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:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Coding!
 

Live masterclass