JavaScript charCodeAt Method | Get the Unicode value of char

  • by

JavaScript charCodeAt() is used to get a Unicode value for a character at a specific position in a string. This method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

string.charCodeAt(index)

Note: If index is out of range, charCodeAt() returns NaN.

The following example returns 65, the Unicode value for A.

'ABC'.charCodeAt(0)  // returns 65

JavaScript charCodeAt example

Simple example code get the Unicode of the first character in a string. Where index of the first character is 0, the second is 1.

<!DOCTYPE html>
<html>
<body>

  <script>
   let text = "HELLO WORLD";
   let code = text.charCodeAt(0);
   console.log(code)
 </script>

</body>
</html>

Output:

JavaScript charCodeAt Method

Get the Unicode of the last character in a string:

<script>
   let text = "HELLO WORLD";
   let code = text.charCodeAt(text.length-1);
   console.log(code)
 </script>

Do comment if you have any doubt or suggestions on this JS basic function.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.