String.charCodeAt() : charCodeAt « String « JavaScript Tutorial






Syntax

string.charCodeAt(num)

The charCodeAt() method returns the ISO-Latin-1 number of the character.

This indexing is done from left to right starting with the 0 (zero) position.

If the num passed is not a valid index in the string, -1 is returned.

<html>
    <head>
      <title>Using the String.charCodeAt() method</title>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    var myString = new String("This is a test");
    var myIndex = prompt("Please enter a number", "");
    var myCharCode = myString.charCodeAt(myIndex);
    var myChar = myString.charAt(myIndex);


    document.write('<b>The string you searched through was: </b>' + myString);
    document.write('<br>The ' + myIndex + ' character in this string is ');

    if (myChar == " "){
      document.write('<space>');
    }else{
      document.write(myChar);
    }
    document.write(' and its ISO-Latin-1 code is ' + myCharCode);
    -->
    </script>
    </body>
    </html>








6.10.charCodeAt
6.10.1.String.charCodeAt()