Algorithm
-
Input:
- Prompt the user to enter a character.
-
Validation:
- Check if the input is a single character. If not, prompt the user again.
- If the input is a single character, proceed to the next step.
-
Conversion:
- Use the
charCodeAt()
method of the string to get the ASCII value of the character.
- Use the
-
Output:
- Display the ASCII value to the user.
Code Examples
#1 Code Example- ASCII Value of Character Using charCodeAt()
Code -
Javascript Programming
// 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}`);
Copy The Code &
Try With Live Editor
Output
Enter a character: a
The ASCII value is: 97
The ASCII value is: 97
#2 Code Example- ASCII Value of Character Using codePointAt()
Code -
Javascript Programming
// 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}`);
Copy The Code &
Try With Live Editor
Output
Enter a character: abc
The ASCII value is: 97
The ASCII value is: 97
Demonstration
JavaScript Program to Find ASCII Value of Character