How to Convert Alphabets to Numbers (and vice versa) in JavaScript

How to Convert Alphabets to Numbers (and vice versa) in JavaScript

Using UTF (ASCII)

UTF (ASCII) tables can be used to convert alphabets to numbers and vice versa. Each character is assigned a decimal number. For example, "A" would be 65, and "a" would be 97. (Check out this blog for more info)

Alphabets to Numbers

String.prototype.charCodeAt()

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

console.log("A".charCodeAt(0))  //65

console.log("ABC".charCodeAt(0))  //65
console.log("ABC".charCodeAt(1))  //66
console.log("ABC".charCodeAt(2))  //67

Numbers to Alphabets

String.fromCharCode()

The static String.fromCharCode() method returns a string created from the specified sequence of UTF-16 code units.

console.log(String.fromCharCode(65)) //A
console.log(String.fromCharCode(66)) //B
console.log(String.fromCharCode(67)) //C

Reference

MDN String.prototype.charCodeAt()

MDN String.fromCharCode()

Convert a character to its ASCII code in JavaScript

Get alphabet position value of a letter [JavaScript]