JavaScript String chartAt

From CoderLearner
Jump to: navigation, search

Description

Return a character in a specified index in a given string.

Syntax

  • index is required and it is the index of the character in the string to be return.
  • Index is started from 0.
string.charAt(index);

Example 1

<html>
    <head>
        <title>JavaScript HTML</title>
    </head>
    <body>
        <script>
            var str="Good old day!";
            var nat = str.charAt(3);
            document.writeln(nat);
        </script>
    </body>
</html>

Result

d

Example 2

The last index is the length-1.

<html>
    <head>
        <title>JavaScript HTML</title>
    </head>
    <body>
        <script>
            var str="Good old day!";
            var nat = str.charAt(str.length-1);
            document.writeln(nat);
        </script>
    </body>
</html>

Result

!