While working with javascript strings and characters, we may encounter a requirement to convert the ASCII value to specific characters. This article demonstrates easy ways to covert the ASCII code to their respective characters using different methods and various example illustrations.
ASCII codes are the numeric value given to the characters and symbols for storing and manipulation by computers. Also, ASCII stands for AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE.
Convert ASCII Code to Characters using fromCharCode()
JavaScript’s fromCharCode() method will return a string created from the stated UTF-16 code units and javaScript characters are UTF-16 code units.
syntax:
String.fromCharCode(numericVal_1) String.fromCharCode(numericVal_1, ....., numericVal_n)
Where, numericVal_1, numericVal_2….numericVal_n are sequence of numbers are UTF-16 code units(range is between 0
 and 65535
(0xFFFF
)) and if range is more than 0xFFFF
, these numeric values are truncated.
Example:-
Get the string values for ASCII codes: 112 and 37 as different characters.
Code:-
Frequently Asked:
- Javascript: Remove first character from a string
- Javascript: substring() method
- Javascript: How to make first letter of a string uppercase
- Remove double quotes from a string in javascript (4 ways)
//function to covert the ASCII Code to String Value function convertToChar(_asciiCode) { return String.fromCharCode(_asciiCode); } //examples let ascii1 = 112; let ascii2 = 37; //usage of the function getASCIICode() console.log(convertToChar(ascii1)); console.log(convertToChar(ascii2));
Output:-
p %
If There are Sequence of UTF-16 Code Units
Example:-
Get the string for sequence of ASCII codes: 74,65,86,65,83,67,82,73,80,84
Code:-
console.log(String.fromCharCode(74,65,86,65,83,67,82,73,80,84));
Output:-
JAVASCRIPT
Note that the method fromCharCode() will return a string and not a String object.
I hope this article helped you get the javasScript characters/ string from the ASCII code. Good Luck !!!