Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript charAt() Method

JavaScript charAt() Method

String Object Reference JavaScript String object

Example

Return the third character in a string:

var str = "HELLO WORLD";

var n = str.charAt(2)

nOutput result:

L

Try it »


Definition and Usage

The charAt() method returns the character at the specified position.

The first character is at position 0, the second at position 1, and so on.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the charAt() method


Syntax

string

.charAt(

index

)

Parameter value

Parameters Description
index Required. A number indicating a position in the string, i.e. the position of the character in the string.

Return Value

Type Description
String Returns the character at the specified position.

Technical Details

JavaScript version: 1.0

More examples

Example

Return the last character in the string:

var str = "HELLO WORLD";

var n = str.charAt(str.length-1);

n Return value:

D

Try it »


String Object Reference JavaScript String object