JavaScript charCodeAt() | Find Unicode of a Character at Specified Index

The JavaScript charCodeAt() method is used when we need to find the Unicode of a character at specified index in a specified string. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p>The Unicode of the character at index no.4 is: <span id="x"></span></p>

   <script>
      let myString = "codescracker.com";
      
      let myCharUnicode = myString.charCodeAt(4);
      document.getElementById("x").innerHTML = myCharUnicode;
   </script>
   
</body>
</html>
Output

The Unicode of the character at index no.4 is:

Since indexing starts with 0, therefore in the string "codescracker.com":

  • 'c' is at index no.0
  • 'o' is at index no.1
  • 'd' is at index no.2
  • and so on

Because the Unicode value of s (which is at index no.4) is 115, therefore you are seeing 115 on the output produced above.

JavaScript charCodeAt() Syntax

The syntax of charCodeAt() method in JavaScript is:

string.charCodeAt(index)

The index parameter is optional. Its default value is 0. Therefore, without index parameter, the charCodeAt() method returns the Unicode value of first character. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

   <script>
      let myStr = "codescracker.com";
      document.getElementById("xyz").innerHTML = myStr.charCodeAt();
   </script>
   
</body>
</html>
Output

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋