JavaScript String at()

Definition and usage

at() Method returns the character at the specified index position from the string.

at() Method with [] has the same effect.

Example

Example 1

Get the first character of the text:

let text = "W3School";
let character = text.at(0);

Try it yourself

Example 2

Get the first character of the text:

let text = "W3School";
let character = text[0];

Try it yourself

Example 3

Get the first character of the text:

let text = "W3School";
let character = text.at();

Try it yourself

Example 4

Get the last character of the text:

let text = "W3School";
let character = text.at(-1);

Try it yourself

Syntax

string.at(index)

Parameter

Parameter Description
index

Optional. Specifies the index (position) of the character to return.

Default value is 0. -1 means return the last character.

Return value

Type Description
String Single character. Returns the character at the specified index position in the string.

Browser support

JavaScript String at() Since March 2022 supported in all browsers:

Chrome Edge Firefox Safari Opera
Chrome 66 Edge 79 Firefox 61 Safari 12 Opera 50
Juli 2021 Juli 2021 Juli 2021 Marts 2022 August 2021