Use charCodeAt() JavaScript function to converts special characters (char) to ASCII in JavaScript.
- charCodeAt() method returns UTF-16 code unit at the specified index.
- codePointAt() method returns a number representing the code point value of the character at the given index.
Different between codePointAt() and charCodeAt() is charCodeAt() method can handle code point which cannot be represented in a single UTF-16 code unit (i.e., characters greater than 0xFFFF).
JavaScript converts special characters to ASCII
HTML example code.
String.charCodeAt() function
This returns an integer between 0 and 65535. The first 128 Unicode code points (0 to 127) match the ASCII character set.
<!DOCTYPE html>
<html>
<body>
<script>
var ch = '$';
var index = 0;
var i = ch.charCodeAt(index);
console.log(i);
</script>
</body>
</html>
String.codePointAt() function
<!DOCTYPE html>
<html>
<body>
<script>
var ch = '$';
var index = 0;
var i = ch.codePointAt(index);
console.log(i);
</script>
</body>
</html>
Output:
Do comment if you have any doubts and suggestions on this JS ASCII char topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version