Latest web development tutorials

JavaScript charAt () method

String Object Reference JavaScript String Object

Examples

Returns a string in the third character:

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

n output:

L

try it"

Definition and Usage

charAt () method returns the character at the specified location.

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


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support charAt () method


grammar

string.charAt( index )

Parameter Value

参数 描述
index 必需。表示字符串中某个位置的数字,即字符在字符串中的位置。

return value

类型 描述
String 返回在指定位置的字符。

technical details

JavaScript version: 1.0


More examples

Examples

Returns 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