Open In App

How to Convert Integer to Its Character Equivalent in JavaScript?

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to convert an integer to its character equivalent using JavaScript.

Method Used: fromCharCode()

This method is used to create a string from a given sequence of Unicode (Ascii is the part of Unicode). This method returns a string, not a string object.

Javascript




let s = () => {
    let str = String.fromCharCode(
                103, 102, 103);
    console.log(str);
};
s(); //print "gfg"


Output

gfg

Approach 1: Integer to Capital characters conversion

Example: Using both from CharCode() and, charCodeAt() methods

Javascript




let example = (integer) => {
    let conversion = "A".charCodeAt(0);
  
    return String.fromCharCode(
        conversion + integer
          
    );
};
  
// Integer should 0<=intger<=25
console.log(example(6)); 
console.log(example(5));
console.log(example(6));


Output

G
F
G

Example: Using only fromCharCode() method

Javascript




let example = (integer) => {
    return String.fromCharCode(
        65 + integer
    ); // Ascii of 'A' is 65
};
console.log(example(6));
console.log(example(5));
console.log(example(6));


Output

G
F
G

Approach 2: Integer to Small characters conversion

Example: Using both fromCharCode() and, charCodeAt() methods:

Javascript




let example = (integer) => {
    let conversion = "a".charCodeAt(0); 
  
    return String.fromCharCode(
        conversion + integer
    );
};
  
// Integer should 0<=intger<=25
console.log(example(6)); 
console.log(example(5));
console.log(example(6));


Output

g
f
g

Example: Using only fromCharCode() method.

Javascript




let example = (integer) => {
    return String.fromCharCode(
        97 + integer);
};
console.log(example(6));
console.log(example(5));
console.log(example(6));


Output

g
f
g




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads