ASCII stands for American Standard Code for Information Interchange.
ASCII codes are basically a standardized set of numerical codes that represent characters in the English alphabet, as well as other common symbols and control characters, such as digits, punctuation marks, and special characters.
Each ASCII code represents a unique character or symbol.
For example, the ASCII code for the letter “A” is 65, and the ASCII code for the letter “a” is 97.
There are a total of 128 ASCII codes numbered from 0 to 127. Each number represents a character or symbol.
In this article, We’ll learn how to convert ASCII codes to characters in JavaScript.
Converting ASCII Codes to Characters
To convert an ASCII code to a character in JavaScript, you can use the built-in String.fromCharCode()
method.
The fromCharCode()
method takes one or more ASCII codes as an argument and returns their corresponding characters.
For example:
const asciiCode = 97; const char = String.fromCharCode(asciiCode); console.log(char);
Output:
"a"
The fromCharCode()
method can also take more than one ASCII codes as argument and return a string that contains the corresponding character to each code.
For example:
const str = String.fromCharCode(97, 98, 99); console.log(str);
Output:
"abc"
If you don’t want to pass the ASCII codes one by one to the fromCharCode()
method as arguments, you can instead put them all in an array and use the spread operator(...
) to spread into a set of arguments:
For example:
const asciiCodes = [97, 98, 99]; const str = String.fromCharCode(...asciiCodes); console.log(str);
Output:
"abc"
You can also get the result of the fromCharCode()
method in the form of an array by using the built-in Array.map()
method.
Here is an example:
const codes = [97, 98, 99]; const charArr = codes.map(code=>String.fromCharCode(code)); console.log(charArr);
Output:
['a', 'b', 'c']
Convert a Character to its ASCII Code
To convert a character back to its ASCII code, you can use the String.charCodeAt()
method.
The charCodeAt()
method takes the index of the character(in the string) whose ASCII code is to be found as an argument and returns an integer value representing the ASCII code of the given character.
If no argument is passed, it returns the ASCII code of the very first character of the string.
For example:
const str = 'abc'; // Returns the ASCII code of the first character const asciiCode = str.charCodeAt(); console.log(asciiCode);
Output:
97
To get the ASCII code of any specific character of the string, you can pass its index as an argument to the charCodeAt()
method.
For example, to get the ASCII code of character ‘b’ in the string “abc”, you can pass its index 1 to the charCodeAt()
method:
const str = 'abc'; // Get ASCII code of character 'b' const asciiCode = str.charCodeAt(1); console.log(asciiCode);
Output:
98
If you are interested in getting the ASCII code of each character of the string, you can loop through the string and push the ASCII code of each character to the array.
For example:
const str = 'abc'; const charCodes = []; for(let i = 0; i < str.length; i++){ charCodes.push(str.charCodeAt(i)); } console.log(charCodes);
Output:
[97, 98, 99]
That’s all for this article. Thanks for reading!