.at()
Published Jul 30, 2023
Contribute to Docs
The .at()
method in JavaScript is used to return the element at a specified index in an array. The index can be either positive or negative (negative indices count from the end of the array).
Syntax
array.at(index);
index
: The index value of the element to be returned.
Example
The example below demonstrates how to use the .at()
function to extract an element from an array based on an index.
const array = [1, 2, 3, 4, 5];const firstElement = array.at(0);console.log(firstElement);
This example results in the following output:
1
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours