Array at() Method in JavaScript

Level 3136 keystrokes

The at() method allows accessing array elements with negative indices counting from the end.

JavaScript code

// Create an array of fruits
1const fruits = ["apple", "banana", "cherry", "date"];
// at(0) returns the first element: "apple"
2console.log(fruits.at(0));
// at(-1) returns the last element: "date"
3console.log(fruits.at(-1));
// at(-2) returns the second-to-last element: "cherry"
4console.log(fruits.at(-2));

Output

apple
date
cherry
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • "× 8
  • .× 6
  • (× 6
  • )× 6
  • ;× 4
  • ,× 3
  • -× 2
  • [× 1
  • ]× 1
  • =× 1

More JavaScript examples at level 3

← JavaScript typing practice