Algorithm


  1. Input:

    • Prompt the user to enter a character.
  2. 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.
  3. Conversion:

    • Use the charCodeAt() method of the string to get the ASCII value of the character.
  4. 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

x
+
cmd
Enter a character: a
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

x
+
cmd
Enter a character: abc
The ASCII value is: 97
Advertisements

Demonstration


JavaScript Program to Find ASCII Value of Character

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way