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






Syntax

string.charAt(num)

The charAt() method returns the character located at the indexed, num, position passed.

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.charAt() method</title>
    </head>
    <body>
    <script language="JavaScript">
    <!--
    var myString = new String("This is a test, small test.");

    var myIndex = prompt("Please enter a number", "");

    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.close();
    -->
    </script>
    </body>
    </html>








6.9.charAt
6.9.1.String.charAt()
6.9.2.Get char in a string by index
6.9.3.Compare the char value