.at()

Published Aug 13, 2024
Contribute to Docs

In JavaScript, the .at() method allows direct access to a character at a specified position in a string. Unlike the .charAt() method, .at() allows both positive and negative integers as an argument, simplifying the operation. Positive integers count indices from the start of the string, while negative integers count from the back. For example, -1 refers to the last character, -2 to the second last, and so on.

Syntax

string.at(index);
  • string: The string from which the character is to be accessed.
  • index: An integer representing the position of the character to be accessed.

Example

Below is an example showcasing the functionality of the .at() method:

const word = 'Codecademy is fun!';
// Accessing the first character in the string
console.log(word.at(0));
// Accessing the last character using a negative index
console.log(word.at(-1));
// Accessing a character at a position that's beyond the string length
console.log(word.at(100));

The above code produces the following output:

C
!
undefined

Note: The .at() method returns undefined for out-of-bound indices, unlike .charAt(), which returns an empty string.

All contributors

Looking to contribute?

Learn JavaScript on Codecademy