How to convert a string to ASCII code in Javascript

In this article, you’ll learn how to convert a given character to ASCII code and how to convert an ASCII code to its associated character in Javascript.

ASCII is a character encoding standard that represents text-based information using numbers. Encoding text-based information to numbers is important because computers can only understand numbers, or more specifically, binaries (i.e. 0s and 1s).

ASCII works with a set of 128 characters consisting of Alphabets, digits, special characters, and control characters. It represents these 128 characters using 7-bit integers from 0 to 127.

This article contains Javascript programs to convert a character to ASCII code and vice versa. Check out the following programs to understand how the conversion is done.

Convert character to ASCII code in Javascript

var s = 'ABC'
var asciiCode = s.charCodeAt(0) // Convert the character at index 0 to ASCII code

console.log(asciiCode) // 65

Convert ASCII code to character in Javascript

var asciiCode = 64
var char = String.fromCharCode(64)

console.log(char) // '@'

Convert a sequence of ASCII values to a string in Javascript

var s = String.fromCharCode(74, 97, 118, 97, 115, 99, 114, 105, 112, 116)

console.log(s) // prints 'Javascript'