JavaScript tutorial:
charAt method

 

Applies to: String Object

The charAt method returns the character at the specified index.

Syntax

strVariable.charAt(index)
"String Literal".charAt(index)

The index argument is the zero-based index of the desired character. Valid values are between 0 and the length of the string minus 1.

Return value

The charAt method returns the character at the specified index from given String.

Example

The charAt method returns a character value equal to the character at the specified index. The first character in a string is at index 0, the second is at index 1, and so forth. Values of index out of valid range return undefined.

The following example illustrates the use of the charAt method:

function getCharAt(str,index)
{ var s;
s = str.charAt(index - 1);
return(s);
} // Get the 12th letter of the alphabet
document.write(getCharAt("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 12));

To run the code, paste it into JavaScript Editor, and click the Execute button.

See also: String Object Methods, String Object Properties