72

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?

1
  • have you done some search before posting. check here Commented Jun 4, 2012 at 9:48

2 Answers 2

134

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is Unicode, not ASCII!!! w3schools.com/jsref/jsref_charcodeat.asp
7

you can try

"str".charCodeAt(0)

2 Comments

@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).
This won't work. it will take only 's'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.