get the ASCII value of char javascript
ASCII is a Character Encoding Scheme. The full form of ASCII is American Standard Code for Information Interchange.
ASCII Encode any character in 7 bits. it was developed for telegraph code.
ASCII is used to represent 128 English characters in Numbers.
ASCII value for A is 65, B is 66, and similar.
ASCII value for a is 97, b is 98, etc
Each character has a unique ASCII value we will use the following two methods to get the ASCII value.
- charCodeAt()
- codePointAt()
Both functions return the Unicode value of the character at a specified index value.
In the title, we say how to get ASCII value but why we are finding Unicode value
In Unicode first 128 characters are ASCII characters. so accessing the first 128 characters will return ASCII Value.
Find ASCII value of a character : Javascript get ASCII value using charCodeAt()
Function charCodeAt() returns a number between 0 to 65535 that represents the UTF-16 code of the character at the specified index.
Capital A ASCII value Example
1 2 3 4 5 6 7 8 | <h2>Get ASCII Value of Character</h2> <p id="msg"></p> <script> var i="a"; var b=i.charCodeAt(0); document.getElementById("msg").innerHTML=b; </script> |
Find ASCII value of a character : Javascript get ASCII value using codePointAt()
Function codePointAt() returns the Unicode value of the character at the specified position.
How to print ASCII value of capital A in JavaScript Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html> <html> <head> <title>Get ASCII Value of Character</title> </head> <body> <h2>Get ASCII Value of Character</h2> <p id="msg"></p> <script> var i="A"; var b=i.codePointAt(0); document.getElementById("msg").innerHTML='Ascii Value of character '+i +' = '+b; </script> </body> </html> |
Program to Get ASCII value of char javascript
1 2 3 4 5 6 7 8 9 10 11 | <h2>Get ASCII Value of Character</h2> Enter Character: <input type="text" id="char"> <button onclick="getChar()">Get ASCII</button> <p id="msg"></p> <script> function getChar(){ var i=document.getElementById("char").value; var b=i.codePointAt(0); document.getElementById("msg").innerHTML='ASCII Value of character '+i +' = '+b; } </script> |
Generating ASCII Values Table in JavaScript
String.fromCharCode() is used here to get characters from ASCII value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | var table; table=' <table id="mtable"> '; table+=' <tr> <th>ASCII Code</th> <th>Character</th> </tr> '; for(i=1;i<=127;i++){ table+=' <tr> <td>'+i+'</td> <td>'+String.fromCharCode(i)+'</td> </tr> '; } table+=' </table> '; document.getElementById("result").innerHTML = table; |
Not all the characters are printable it is showing a box.
ASCII value of alphabets in javaScript
The ASCII value of Capital Letters starts from 65 and ends at 90. Small case letters start from 97 and end at 122.
To print Capital Letters start the loop from 65 and end it at 90 and for small latter start with position 97 and at 122.
ASCII value of Capital Letter (aSCII code of capital a to z)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | var table; table=' <table id="mtable"> '; table+=' <tr> <th>ASCII Code</th> <th>Character</th> </tr> '; for(i=65;i<=90;i++){ table+=' <tr> <td>'+i+'</td> <td>'+String.fromCharCode(i)+'</td> </tr> '; } table+=' </table> '; document.getElementById("result").innerHTML = table; |
ASCII value of Small Letter (aSCII code of Small a to z)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | var table; table=' <table id="mtable"> '; table+=' <tr> <th>ASCII Code</th> <th>Character</th> </tr> '; for(i=97;i<=122;i++){ table+=' <tr> <td>'+i+'</td> <td>'+String.fromCharCode(i)+'</td> </tr> '; } table+=' </table> '; document.getElementById("result").innerHTML = table; |
ASCII value of numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | var table; table=' <table id="mtable"> '; table+=' <tr> <th>ASCII Code</th> <th>Character</th> </tr> '; for(i=49;i<=57;i++){ table+=' <tr> <td>'+i+'</td> <td>'+String.fromCharCode(i)+'</td> </tr> '; } table+=' </table> '; document.getElementById("result").innerHTML = table; |
Here we showed how Javascript gets the ASCII value of char can work to get ASCII Values.
charCodeAt() and codePointAt() methods are used to get ASCII values
String.fromCharCode() converts ASCII code to character conversion.
The ASCII value of 1 is 49
The ASCII value of A is 65
use charCodeAt() and codePointAt() methods.
The ASCII value of A is 97
The ASCII value of Capital A to Z is 65 to 90