In this article, we will explore the ASCII values in JavaScript.
ASCII
It represents the American Standard Code for Information Interchange. This character encoding scheme is employed to facilitate communication between computer systems. It assigns standard numerical values to characters such as digits, letters, and more.
The ASCII character set comprises 128 distinct characters. This collection includes both lowercase and uppercase alphabets ranging from A to Z, digits from 0 to 9, as well as punctuation marks and various other symbols.
We will understand how to find the ASCII value of a character in JavaScript . There are various methods to find the ASCII value of a character.
- Utilizing charCodeAt
- Utilizing codePointAt
- Utilizing Array.from method with charCodeAt
- Utilizing Array.from method with codePointAt
- Utilizing the charCodeAt(index) method
We will explore each method for determining the ASCII value of a character in a step-by-step manner.
Utilizing the charCodeAt
The charCodeAt method is employed to receive an index value as its argument and yields an integer that represents its UTF-16 code, which is a number ranging from 0 to 65535. By default, the index value for the charCodeAt method is set to 0.
Demonstration:
In this example, we will employ the charCodeAt function to determine the ASCII value corresponding to a specific character.
Code:
let ch = "n";
let output = ch.charCodeAt();
console.log(`The ASCII value of a character "n" is ${output}.`);
Output:
The result illustrates the ASCII value corresponding to the character "n," which is obtained through the use of the charCodeAt method.
Utilizing the codePointAt
The codePointAt method is used to retrieve the ASCII value of a specified character. It accepts a string comprised of three characters as its argument and yields the corresponding code point value. By default, the index value for codePointAt is set to 0.
Demonstration:
We will employ the codePointAt function to retrieve the ASCII value of a specific character.
Code:
let ch = "@";
let output = ch.codePointAt();
console.log(`The ASCII value of a character "@" is ${output}.`);
Output:
In the result presented below, we can observe the ASCII value corresponding to the character "@", which is obtained using the codePointAt method.
Utilizing Array.from method with charCodeAt
We will retrieve the ASCII values of an entire string by employing the Array.from method in conjunction with the charCodeAt function.
Demonstration:
We will explore how to determine the ASCII value of a character by employing the Array.from method in conjunction with the charCodeAt method. The Array.from method converts the string into an array, after which each element of the array is converted into its corresponding ASCII value.
Code:
let inputString = "Welcome";
let arr = Array.from(inputString, (char) => {
return char.charCodeAt(0);
});
console.log(
"ASCII values of a full string in '" +
inputString +
"': " +
arr.join(", ")
);
Output:
Let us observe the result that reveals the ASCII values for each character in the complete string "Welcome." This is accomplished through the combination of the Array.from method and the charCodeAt function.
Utilizing Array.from method with the codePointAt
To retrieve the ASCII values of an entire string, we can employ the Array.from method in conjunction with the codePointAt function.
Demonstration:
In this example, we will employ the Array.from method in conjunction with the codePointAt function to obtain the ASCII value of a specific character. The Array.from function serves to transform the string into an array, after which each element in the array is converted into its corresponding ASCII value.
Code:
let fullString = "Hello";
let Str = Array.from(fullString, (char) => {
return char.codePointAt(0);
});
console.log(
"ASCII values of a full string in '" +
fullString +
"': " +
Str.join(", ")
);
Output:
In the following output below, we can distinctly observe the ASCII values corresponding to each character in the complete string "Hello." This is accomplished through the use of the Array.from method in conjunction with the codePointAt method.
Utilizing the charCodeAt(index) method
The ASCII value at a specific index can be obtained by using the charCodeAt(index) method.
Demonstration:
In this demonstration, we will utilize the charCodeAt(index) method to retrieve the ASCII value of a character located at a specified index.
Code:
const sentence = 'Finding the ASCII value of a particular index.';
const index = 10;
console.log(
`Character code of ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(
index,
)}`,
);
Output:
We can distinctly observe the ASCII value corresponding to the character located at index 10.
Conclusion:
In this article, we have explored the concept of JavaScript ASCII values. ASCII, which stands for the American Standard Code for Information Interchange, serves as a protocol for data communication between different devices. We have examined various techniques to determine the ASCII value of a character, including the use of the charCodeAt method, the codePointAt method, the Array.from method in conjunction with charCodeAt, the Array.from method with codePointAt, and the charCodeAt(index) method.