Home » JavaScript » JavaScript Convert Character to ASCII and Vice Versa

About Venkatesh Nukala

Venkatesh Nukala
Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com

JavaScript Convert Character to ASCII and Vice Versa

A quick guide to convert a character into ASCII code in javascript and vice versa.

1. Overview

In this tutorial, We’ll learn how to convert a character into ASCII code in javascript and vice versa.

For character A, its ASCII code is 65

B = 66

a = 97

b = 98

0 = 48

For each character, there will be a unique ASCII code.

There are two ways to convert character to ASCII code in javascript using the below methods.



To convert from Code Point ASCII code to character in javascript, use the below methods.



2. JavaScript Character to ASCII Using String.charCodeAt() Function

charCodeAt() method takes the index of the character and returns UTF-16 code for the given index.

The first 128 Unicode points from 0 to 127 are used to match the ASCII character set.

Look at the below program.

var chracter = 'B';
var index = 0;
var asciiCode = chracter.charCodeAt(index);



ASCII code for character B is 66. The same has been produced in the output console.

3. JavaScript Character to ASCII Using String. codePointAt() Function

When the String characters are having special characters such as codepoints then the charCodeAt() method does not work. In this case, you need to use the codePointAt() method.

codePointAt() method returns the integer value in between 0 and 65535.

Look at the below example script using codePointAt() function.

const sentence = 'hello welcome to the javascript char to ascii post';

const index = 7;

console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);



"The character code 101 is equal to e"

4. JavaScript ASCII to Character Using String fromCharCode()

If you want to get the character from the ASCII codes, use the String.fromCharCode() method from the String object.

console.log(String.fromCharCode(65, 66)); 
console.log(String.fromCharCode(67, 68)); 
console.log(String.fromCharCode(69, 70));

Any no of arguments can be passed to
fromCharCode() method and all characters are returned as String object by concatenating all characters of the given ASCII codes.


Look at the output how the fromCharCode() method has generated the output.


5. JavaScript codepoints to a character using String.fromCodePoint()

String.fromCodePoint() is used to get the string from the given set of code points sequence.

This method also takes any no of arguments. Look at the below example with different arguments.

console.log(String.fromCodePoint(9731, 9733));
console.log(String.fromCodePoint(9735, 9737));
console.log(String.fromCodePoint(9731, 9733, 9842));
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2F804));



6. Conclusion

In this article, we’ve seen how to convert the character to ascii code in java script and vice versa with examples.

String API

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: JavaScript Convert Character to ASCII and Vice Versa

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments