JavaScript Program to Find ASCII Value of Character
ASCII (American Standard Code for Information Interchange) values are numerical codes assigned to characters, enabling computers and electronic devices to process text. Each character corresponds to a unique ASCII value.
For example:
-
'A' has an ASCII value of 65.
-
'a' has an ASCII value of 97.
-
'0' has an ASCII value of 48.
This guide explores various ways to handle ASCII values in JavaScript, including how to retrieve ASCII values, print them, with explanation and output.
JavaScript Programs to Find ASCII Value of a Character Using charCodeAt
The charCodeAt method is the simplest way to find the ASCII value of a character.
Code
function findAsciiValue(character) {
return character.charCodeAt(0);
}
// Test the function
const char = 'A';
console.log(`The ASCII value of '${char}' is:`, findAsciiValue(char));
Output
The ASCII value of 'A' is: 65
Explanation
The charCodeAt method returns the Unicode value of the character at the specified index, which corresponds to the ASCII value for standard characters.
JavaScript Program to Print ASCII Values of String Characters
This program prints the ASCII values for all characters in a string.
Code
function printAsciiValues(str) {
for (let i = 0; i < str.length; i++) {
console.log(`The ASCII value of '${str[i]}' is: ${str.charCodeAt(i)}`);
}
}
// Test the function
const inputString = "Hello";
printAsciiValues(inputString);
Output
The ASCII value of 'H' is: 72
The ASCII value of 'e' is: 101
The ASCII value of 'l' is: 108
The ASCII value of 'l' is: 108
The ASCII value of 'o' is: 111
Explanation
-
The loop iterates over each character in the string and uses charCodeAt to find and print its ASCII value.
-
This method demonstrates how to print ASCII value in JavaScript for multiple characters.
Converting ASCII Values to Characters in JavaScript Using String.fromCharCode
To convert an ASCII value back to a character, use the String.fromCharCode method.
Code
function getCharacterFromAscii(asciiValue) {
return String.fromCharCode(asciiValue);
}
// Test the function
const asciiValue = 65;
console.log(`The character for ASCII value ${asciiValue} is:`, getCharacterFromAscii(asciiValue));
Output
The character for ASCII value 65 is: A
Explanation
-
The String.fromCharCode method converts the ASCII value to its corresponding character.
-
This is useful for tasks where you need to get a character from ASCII value in JavaScript.
ASCII Value of Character in JavaScript Using codePointAt()
The codePointAt method provides a Unicode code point for a character, which is equivalent to its ASCII value for standard characters.
Code
function findAsciiUsingCodePoint(character) {
return character.codePointAt(0);
}
// Test the function
const charCode = 'A';
console.log(`The ASCII value of '${charCode}' using codePointAt() is:`, findAsciiUsingCodePoint(charCode));
Output
The ASCII value of 'A' using codePointAt() is: 65
Explanation
-
The codePointAt method is useful for working with characters represented by Unicode code points, including extended characters.
-
For standard ASCII characters, codePointAt produces the same result as charCodeAt.
Concepts Used in Above Programs
charCodeAt Method
-
Retrieves the ASCII value of a character at a specified index in a string.
Loops
-
Used to iterate through strings and print ASCII values for each character.
String.fromCharCode Method
-
Converts an ASCII value back to its corresponding character.
codePointAt Method
-
Retrieves the Unicode code point for a character, equivalent to its ASCII value for standard characters.