JavaScript
Coding
ASCII Code
Character Conversion
Programming

Convert character to ASCII code in JavaScript

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In JavaScript, converting a character to its corresponding ASCII code (American Standard Code for Information Interchange) is a useful operation, often required when processing text data. This conversion allows developers to analyze or manipulate characters based on their ASCII values. The ASCII coding scheme associates a numeric value to each symbol or character in the English alphabet, along with a variety of control characters and special symbols.

How to Convert Character to ASCII in JavaScript

Using the charCodeAt() Method

The primary method used in JavaScript to convert a character into its ASCII code is charCodeAt(). This method is a property of string objects and returns an integer representing the Unicode code point value of the character at a specified position in a string.

Syntax:

javascript
string.charCodeAt(index)
  • string: This is the string from which a character's ASCII code is derived.
  • index: This is the position of the character in the string. The index of the first character is 0.

Example:

javascript
let str = 'Hello';
let asciiCode = str.charCodeAt(0);
console.log(asciiCode); // Outputs: 72

In this example, the ASCII code of 'H' is 72.

Considerations for UTF-16

While charCodeAt() is traditionally thought of in terms of ASCII, it technically returns a UTF-16 code unit in modern JavaScript implementations. For characters within the Basic Multilingual Plane (BMP), this is identical to their ASCII value if the character is also represented in ASCII (i.e., English letters, digits, and basic punctuation).

However, for characters outside this range (e.g., emoji, complex script characters), charCodeAt() returns the leading surrogate pair value, which may not correspond to traditional ASCII values. In such cases, codePointAt() might be used to get the full code point if dealing with Unicode characters extensively.

ASCII Table and JavaScript Conversion

Below is a summarized table of the ASCII codes for English letters:

CharacterASCII Code
A65
B66
C67
......
Z90
a97
b98
c99
......
z122

Handling Multiple Characters and Strings

If you need to derive ASCII codes from multiple characters in a string, it's common to loop through the string using a for loop or any higher-order array methods if converting each character into an array is easier.

Example:

javascript
1let text = "Hello, World!";
2let asciiCodes = [];
3
4for (let i = 0; i < text.length; i++) {
5    asciiCodes.push(text.charCodeAt(i));
6}
7
8console.log(asciiCodes);
9// Outputs: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

Conclusion

Understanding how to convert characters to ASCII codes in JavaScript is important, especially when handling data validations, encoding, or when performing operations based on character codes. While charCodeAt() is suitable for most practical purposes within the scope of ASCII characters, remember to consider codePointAt() for full Unicode support, especially when dealing with non-standard characters and symbols in international applications.

Through the practical use of these string methods, you can effectively handle and manipulate textual data in your JavaScript applications, ensuring broader functionality and compatibility across different data processing scenarios.


Course illustration
Course illustration

All Rights Reserved.