If you have only one char and not a string, you can use:
'\n'.charCodeAt();
omitting the 0…
It used to be significantly slower than 'n'.charCodeAt(0), but I’ve tested it now and I do not see any difference anymore (executed 10 billions times with and without the 0). Tested for performance only in Chrome and Firefox.
JavaScript stores strings as UTF-16 (double byte) so if you want to ignore the second byte just strip it out with a bitwise & operator on 0000000011111111 (ie 255):
You can enter a character and get Ascii Code Using this Code
For Example Enter a Character Like A
You Get Ascii Code 65
function myFunction(){
var str=document.getElementById("id1");
if (str.value=="") {
str.focus();
return;
}
var a="ASCII Code is == > ";
document.getElementById("demo").innerHTML =a+str.value.charCodeAt(0);
}
While the other answers are right, I prefer this way:
Then, to use it, simply:
I am using this for a small shortcut system:
And you can even use it inside map() or other methods:
If you have only one char and not a string, you can use:
omitting the 0…
It used to be significantly slower than
'n'.charCodeAt(0)
, but I’ve tested it now and I do not see any difference anymore (executed 10 billions times with and without the 0). Tested for performance only in Chrome and Firefox.For those that want to get a sum of all the ASCII codes for a string:
Or, ES6:
JavaScript stores strings as
UTF-16
(double byte) so if you want to ignore the second byte just strip it out with a bitwise&
operator on0000000011111111
(ie 255):You can enter a character and get Ascii Code Using this Code
For Example Enter a Character Like A
You Get Ascii Code 65
To convert a String to a cumulative number:
Use case:
Say you want to generate different background colors depending on a username:
To ensure full Unicode support and reversibility, consider using:
This will ensure that when testing characters over the UTF-16 limit, you will get their true code point value.
e.g.
Using
charCodeAt()
The following example returns 65, the Unicode value for
A
.'ABC'.charCodeAt(0)
// returns 65