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)
Author by
Anand Murugan
Updated on July 20, 2022Comments
-
Anand Murugan almost 2 years
Possible Duplicate:
Convert character to ASCII code in Javascriptmy requirement is to get the ASCII value of the alphabet letters... Can anyone suggest how to do this in JavaScript?
-
Andrew almost 8 yearsThis is Unicode, not ASCII!!! w3schools.com/jsref/jsref_charcodeat.asp
-
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 over 4 yearsThis won't work. it will take only 's'