Last updated on Dec 7, 2020 by Suraj Sharma
In this tutorial you will learn how you can convert an ASCII code to a character and vice versa in JavaScript,
String.fromCharCode()
method is used to get the character corresponding to an ascii code
String.fromCharCode(65) // "A"
String.fromCharCode(97) // "a"
You can pass multiple arguments to the fromCharCode()
method to get a string of characters from the ascii codes
String.fromCharCode(65,66,67) // "ABC"
const arr = [74, 97, 118, 97, 83, 99, 114, 105, 112, 116];
String.fromCharCode(...arr) // "JavaScript"
To convert a character to it’s ascii code you can use the String.charCodeAt()
method
'B'.charCodeAt(0) // 66
'ABC'.charCodeAt(2) // 67
The charCodeAt
method accepts index of the string as an argument
Related Solutions
Rate this post
Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.