65

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?

Community
  • 1
  • 1
Anand Murugan
  • 767
  • 2
  • 7
  • 12
  • have you done some search before posting. check [here](http://stackoverflow.com/questions/94037/convert-character-to-ascii-code-in-javascript) – Nikson Kanti Paul Jun 04 '12 at 09:48

2 Answers2

123

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.

Nhan
  • 3,595
  • 6
  • 30
  • 38
ioseb
  • 16,625
  • 3
  • 33
  • 29
7

you can try

"str".charCodeAt(0)
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
  • 1
    @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). – Nateowami Apr 30 '17 at 14:56
  • 3
    This won't work. it will take only 's' – Oguz Dec 01 '19 at 10:37