JavaScript Program to Find ASCII Value of Character

To understand this example, you should have the knowledge of the following JavaScript programming topics:


ASCII stands for American Standard Code for Information Interchange.

ASCII is a numeric value that is given to different characters and symbols for computers to store and manipulate. For example, the ASCII value of the letter 'A' is 65.

Resource: ASCII chart of all 127 characters in JavaScript.


Example 1: ASCII Value of Character Using charCodeAt()

// program to find the ASCII value of a character

// take input from the user
const string = prompt('Enter a character: ');

// convert into ASCII value
const result = string.charCodeAt(0);

console.log(`The ASCII value is: ${result}`);

Output

Enter a character: a
The ASCII value is: 97

In the above program, the charCodeAt() method is used to find the ASCII value of a character.

The charCodeAt() method takes in an index value and returns an integer representing its UTF-16 (16-bit Unicode Transformation Format) code.

If you don't pass the index value, the default index value will be 0. If the index value is out of range, it gives NaN.


Example 2: ASCII Value of Character Using codePointAt()

// program to find the ASCII value of a character

// take input from the user
const string = prompt('Enter a character: ');

// convert into ASCII value
const result = string.codePointAt(0);

console.log(`The ASCII value is: ${result}`);

Output

Enter a character: abc
The ASCII value is: 97

In the above program, the codePointAt() method is used to find the ASCII value of a character.

The codePointAt() method returns a Unicode code point value.

In the above program, the user inputs three-character string abc. However, the index 0 is passed to the codePointAt() method. This gives the ASCII value of the first character (here a).

If you don't pass the index value, the default index value will be 0. If the index value is out of range, it gives undefined.

Before we wrap up, let’s put your knowledge of JavaScript Program to Find ASCII Value of Character to the test! Can you solve the following challenge?

Challenge:

Write a function to return the ASCII value of a character.

  • ASCII (American Standard Code for Information Interchange) values are numeric representations of characters.
  • For example, the ASCII value of 'A' is 65, and it increments by one for each subsequent letter, making 'B'66, and so on.
  • Return the ASCII value of the input character ch.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges