How to get the ASCII value in JavaScript for the characters

118,414

Solution 1

Here is the example:

var charCode = "a".charCodeAt(0);
console.log(charCode);

Or if you have longer strings:

var string = "Some string";

for (var i = 0; i < string.length; i++) {
  console.log(string.charCodeAt(i));
}

String.charCodeAt(x) method will return ASCII character code at a given position.

Solution 2

you can try

"str".charCodeAt(0)
Share:
118,414
Anand Murugan
Author by

Anand Murugan

Updated on July 20, 2022

Comments

  • Anand Murugan
    Anand Murugan almost 2 years

    Possible Duplicate:
    Convert character to ASCII code in Javascript

    my requirement is to get the ASCII value of the alphabet letters... Can anyone suggest how to do this in JavaScript?

  • Andrew
    Andrew almost 8 years
    This is Unicode, not ASCII!!! w3schools.com/jsref/jsref_charcodeat.asp
  • Nateowami
    Nateowami about 7 years
    @Andrew ASCII is a subset of Unicode, so it's actually the same in this case. (Actually, Unicode is not an encoding, and ASCII is, so in that sense they are different. But for the purposes of charCodeAt, there's no difference).
  • Oguz
    Oguz over 4 years
    This won't work. it will take only 's'