Conversion between character and integer in JavaScript
This post will discuss how to convert a character to an integer in JavaScript, and vice versa.
There are several ways to convert a character to an integer in JavaScript, and vice versa, depending on our needs and preferences. A character is a single unit of text, such as a letter, a digit, or a symbol. An integer is a whole number that can be positive, negative, or zero. To convert a character to an integer, we need to find the numeric value that corresponds to the character in a given encoding system, such as ASCII or Unicode. To convert an integer to a character, we need to find the character that corresponds to the numeric value in a given encoding system.
Here are some of the most common and easy-to-use functions for converting characters and integers in JavaScript:
1. Using String.fromCharCode()
function
The String.fromCharCode() is a static function of the String
object that returns a string created from a sequence of Unicode values. We can use this function to convert an integer to a character by passing the integer as an argument. For example, if we want to convert the integer 65
to the character 'A'
, we can write:
1 2 3 |
let num = 65; let char = String.fromCharCode(num); console.log(char); // A |
We can also pass multiple integers as arguments to create a string of characters. Here’s an example:
1 2 3 |
let nums = [72, 101, 108, 108, 111]; let chars = String.fromCharCode(...nums); console.log(chars); // 'Hello' |
2. Using String.charCodeAt()
function
The String.charCodeAt() is an instance function of the String
object that returns the Unicode value of the character at a given index. We can use this function to convert a character to an integer by calling it on a string and passing the index of the character as an argument. For example, if we want to convert the character 'A'
to the integer 65
, we can write:
1 2 3 |
let char = 'A'; let num = char.charCodeAt(0); console.log(num); // 65 |
We can also loop through a string and get the Unicode values of each character. Here’s an example:
1 2 3 4 5 6 |
let chars = 'Hello'; let nums = []; for (let i = 0; i < chars.length; i++) { nums.push(chars.charCodeAt(i)); } console.log(nums); // [72, 101, 108, 108, 111] |
3. Using parseInt()
function
The parseInt() is a global function that parses a string and returns an integer in a specified radix (base). We can use this function to convert a character to an integer by passing the character as a string and the radix as 10
(decimal). For example, if we want to convert the character '9'
to the integer 9
, we can write:
1 2 3 |
let char = '9'; let num = parseInt(char, 10); console.log(num); // 9 |
However, this function will only work for characters that represent digits from 0
to 9
. If we pass any other character, it will return NaN
(Not a Number). Here’s an example:
1 2 3 |
let char = 'A'; let num = parseInt(char, 10); console.log(num); // NaN |
4. Using Number()
function
The Number()
is a global function that converts any value to a number. We can use this function to convert a character to an integer by passing the character as an argument. For example, if we want to convert the character “9” to the integer 9
, we can write:
1 2 3 |
let char = '9'; let num = Number(char); console.log(num); // 9 |
However, this function will also only work for characters that represent digits from 0
to 9
. If we pass any other character, it will return NaN (Not a Number). Here’s an example:
1 2 3 |
let char = 'A'; let num = Number(char); console.log(num); // NaN |
5. Using Number.toString()
function
The toString()
is an instance function of the Number
object that converts a number to a string representation in a specified radix (base). We can use this function to convert an integer to a character by calling it on an integer and passing the radix as 10
(decimal). For example, if we want to convert the integer 9
to the character “9”, we can write:
1 2 3 |
let num = 9; let char = num.toString(10); console.log(char); // '9' |
However, this function will only work for integers that represent digits from 0
to 9
. If we pass any other integer, it will return a string of multiple characters. Here’s an example:
1 2 3 |
let num = 65; let char = num.toString(10); console.log(char); // '65' |
That’s all about converting characters to integers in JavaScript, and vice versa.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)