Convert Integer to its Character Equivalent in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
2 min

banner

# Convert an Integer to its Character Equivalent in JavaScript

To convert an integer to its character equivalent:

  1. Use the charCodeAt() method to get the character code of the letter a.
  2. Call the fromCharCode() method with the sum of the integer and the character code.
index.js
function intToChar(int) { // ๐Ÿ‘‡๏ธ for Uppercase letters, replace `a` with `A` const code = 'a'.charCodeAt(0); console.log(code); // ๐Ÿ‘‰๏ธ 97 return String.fromCharCode(code + int); } console.log(intToChar(0)); // ๐Ÿ‘‰๏ธ "a" console.log(intToChar(4)); // ๐Ÿ‘‰๏ธ "e"

convert integer to its character equivalent in javascript

The code for this article is available on GitHub

We used the String.charCodeAt() to get the character code of the letter a.

The character code of a is 97.

index.js
console.log('a'.charCodeAt(0)); // ๐Ÿ‘‰๏ธ 97

The only parameter the charCodeAt method takes is the index of the character in the string for which to get the character code.

# Convert an Integer to its Uppercase Character Equivalent

If you need to convert the integer to an uppercase character, get the character code of the letter A instead (65).

index.js
function intToChar(int) { const code = 'A'.charCodeAt(0); console.log(code); // ๐Ÿ‘‰๏ธ 65 return String.fromCharCode(code + int); } console.log(intToChar(0)); // ๐Ÿ‘‰๏ธ "A" console.log(intToChar(4)); // ๐Ÿ‘‰๏ธ "E"

convert integer to its uppercase character equivalent

The code for this article is available on GitHub

Notice that we called the String.charCodeAt method on a capital letter A to convert the integer to its uppercase character equivalent.

The last step is to use the String.fromCharCode() method to get the character equivalent of the integer.

The method takes one or more numbers as parameters and returns a string containing the code units for the numbers.

By adding the character code of the letter a to the integer, we start counting from 0, where 0 is a, 1 is b, etc.

You can convert the character back to an integer by using the charCodeAt method.

# Convert the character back to its integer equivalent

To convert a character back to its integer equivalent:

  1. Use the charCodeAt method to get the UTF-16 code unit of the letter a.
  2. Use the charCodeAt method to get the code unit of the character.
  3. Subtract the code unit of the letter a from the code unit of the character.
index.js
function charToInt(char) { const code = 'a'.charCodeAt(0); return char.charCodeAt(0) - code; } console.log(charToInt('a')); // ๐Ÿ‘‰๏ธ 0 console.log(charToInt('e')); // ๐Ÿ‘‰๏ธ 4

convert character back to its integer equivalent

The code for this article is available on GitHub

The first step is to get the UTF-16 code unit of the letter a.

Then, we subtract the character code of the letter a from the character code of the supplied character.

The function returns the integer equivalent of the character.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev