JavaScript charAt()Method

String 对象参考手册JavaScript String Object

Example

Return the third character in the string:

var str = "HELLO WORLD";
var n = str.charAt(2)

nOutput result:

L

Try it Yourself »

Definition and Usage

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

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


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the charAt() method


Syntax

string.charAt(index)

Parameter Values

Parameter Description
index Required. A number representing 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);

nReturn value:

D

Try it Yourself »


String 对象参考手册JavaScript String Object

Other Extensions