• JavaScript Video Tutorials

JavaScript - Array at() Method



In JavaScript the Array.at() method is used to retrieve an element at a specified index in an array. We can pass both positive and negative values to this function: Positive integers count from the first item in the array, while negative integers count backward from the last item.

If we provide out-of-range index value as an argument, it won't throw an error, instead it it returns "undefined".

Syntax

Following is the syntax of JavaScript Array at() method −

array.at(index);

Parameters

This method accepts only one parameter. The same is described below −

  • The "index" parameter represents the position of the desired element, and the method returns the element found at that index.

Return value

This method returns the element of the provided index in the array.

Examples

Example 1

In the following example, we are using the JavaScript Array at() function to return the fifth element of the array −

<html>
<body>
   <p id="demo"></p>
   <script>
      const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur", "Wolf"];
      let result = animals.at(4);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

As we can see output below, this method returned a new array (result) with the fifth element of animals array.

Dinosaur

Example 2

In this example, we are fetching the sixth element of the array using the "[]" square bracket notation −

<html>
<body>
   <p id="demo"></p>
   <script>
      const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur", "Wolf"];
      let result = animals[5];
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

The program will return the sixth element from the array.

Wolf

Example 3

If we do not pass any value to the at() function, it will return the first element from the specified array −

<html>
<body>
   <p id="demo"></p>
   <script>
      const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur", "Wolf"];
      let result = animals.at();
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

When we execute the above program, it will return first element of the array.

Lion

Example 4

If we pass 0 as an argument to this function, it will return the first element from the array −

<html>
<body>
   <p id="demo"></p>
   <script>
      const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur", "Wolf"];
      let result = animals.at(0);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

As we can see the output, this method returned first element of the array.

Lion

Example 5

In this example, we are returning the last second element from the right side of the array. We are doing that by passing a negative integer value as an argument to this function −

<html>
<body>
   <p id="demo"></p>
   <script>
      const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur", "Wolf"];
      let result = animals.at(-2);
      document.getElementById("demo").innerHTML = result;
   </script>
</body>
</html>

As we can see the output, this method returned last second element from the right side of the array.

Dinosaur
Advertisements