Signup/Sign In
LAST UPDATED: FEBRUARY 15, 2023

JavaScript Program to Find ASCII Value of Character

JavaScript is a popular programming language because of its adaptability and flexibility. It is a popular language for server-side programming and is used to create dynamic, interactive web pages. The ability to find out an ASCII value for a character is one of JavaScript's many features. American Standard Code for Information Interchange (ASCII) is a character encoding standard that converts textual characters into a series of numeric values.

By knowing an ASCII value, programmers can easily compare and manipulate text within programs. This piece will focus on a JavaScript script for determining an ASCII code for a given character. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

What is ASCII?

In the past, electronic communications typically used a character encoding standard known as American Standard Code for Information Interchange (ASCII). It specifies a range of numbers that can be used to represent letters, numbers, and other symbols commonly used in written communication.

Using a range from 0 to 127, we can represent each character digitally. Originally released in 1963, the American Standard Code for Information Interchange (ASCII) is still the gold standard for character encoding.

Although Unicode, a more recent and flexible character encoding standard that supports a much wider range of characters and symbols, has largely replaced it, UTF-8 is still widely used in a variety of applications today.

For example, the ASCII value of the letter 'A' is 65.

Method 1: Program to Find ASCII Value of Character Using charCodeAt()

The charCodeAt() method takes in an index value and returns an integer representing its UTF-16 code. If you don't pass the index value, the default index value will be 0 and at any point it is out of range, it gives NaN.

// Find the ASCII value of a character Programs

const character= prompt('Enter a character: ');

// convert into ASCII value
const res = character.charCodeAt(0);

console.log("The ASCII value is: " , res);


Enter a character: c
The ASCII value is: 99

Method 2: Program to Find ASCII Value of Character Using codePointAt()

The codePointAt() method returns a Unicode code point value. The user inputs a three-character string xyz. However, the index 0 is passed to the codePointAt() method and will return the first character of the value here (in this case x). If you don't pass the index value, the default index value will be 0 and if the index value is out of range, it will give undefined.

// Find the ASCII value of a character Programs

const character = prompt('Enter a character: ');

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

console.log("The ASCII value is: "+ res);


Enter a character: cde
The ASCII value is: 99

Conclusion

In conclusion, determining an element's ASCII value is a valuable task in JavaScript. It provides a straightforward numeric representation of characters and facilitates manipulation and comparison by programmers.

This article's easy-to-follow code for determining a character's ASCII value is useful for a wide variety of programming projects. No matter how much experience you have with JavaScript, knowing how to work with character encoding, including the range of ASCII values, is a valuable skill.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.