How to convert ASCII code to a character in JavaScript

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,



Convert ASCII code to a character


String.fromCharCode() method is used to get the character corresponding to an ascii code


Example


String.fromCharCode(65) // "A"

String.fromCharCode(97) // "a"


Convert multiple ascii code to a string


You can pass multiple arguments to the fromCharCode() method to get a string of characters from the ascii codes


Example


String.fromCharCode(65,66,67) // "ABC"

const arr = [74, 97, 118, 97, 83, 99, 114, 105, 112, 116];

String.fromCharCode(...arr) // "JavaScript"


Convert a character to an ascii code


To convert a character to it’s ascii code you can use the String.charCodeAt() method


Example


'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 a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.