JavaScript String at()

Definition and Usage

at() Method returns the character at the specified index 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. The index (position) of the character to return.

Default value is 0. -1 indicates returning the last character.

Return Value

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

Browser Support

JavaScript String at() Support available in all browsers since March 2022:

Chrome Edge Firefox Safari Opera
Chrome 66 Edge 79 Firefox 61 Safari 12 Opera 50
Ιούλιος 2021 Ιούλιος 2021 Ιούλιος 2021 Μάρτιος 2022 Αύγουστος 2021