charAt

Returns the specified character from the string.

Applies to

String

Syntax

charAt(index)

Parameters

index

An integer between 0 and 1 less than the length of the string.

Description

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string called stringName is stringName.length - 1. If the index you supply is out of range, JavaScript returns an empty string.

Example

The following example displays characters at different locations in the string "Brave new world":

var anyString="Brave new world"
Console.Write("The character at index 0 is " + anyString.charAt(0))
Console.Write("The character at index 1 is " + anyString.charAt(1))
Console.Write("The character at index 2 is " + anyString.charAt(2))
Console.Write("The character at index 3 is " + anyString.charAt(3))
Console.Write("The character at index 4 is " + anyString.charAt(4))

These lines display the following:

The character at index 0 is B
The character at index 1 is r
The character at index 2 is a
The character at index 3 is v
The character at index 4 is e

See also

String:indexOf, String.lastIndexOf, String.split