Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

JavaScript Array at() Method

Posted on December 17, 2024 By Admin No Comments on JavaScript Array at() Method

The Array.at() method is a built-in JavaScript function that allows you to access elements in an array using their index. Its ability to handle negative indices, making it easier to retrieve elements from the end of an array.

Syntax

arr.at(index);

Parameters

ParameterDescription
indexThe index of the element to be accessed. A negative value counts backward from the end of the array.

Return Value

The Array.at() method returns the element at the specified index, if it exists. undefined if the index is out of bounds.

Example 1: Accessing Elements by Index – The Array.at() is used to access elements at specific positions in the array.

let arr = [10, 20, 30, 40, 50];

console.log(arr.at(0)); // Output: 10
console.log(arr.at(2)); // Output: 30

Example 2: Using Negative Indexes – Negative indices retrieve elements from the end of the array.

let arr = [10, 20, 30, 40, 50];

console.log(arr.at(-1)); // Output: 50 (Last element)
console.log(arr.at(-3)); // Output: 30 (Third-to-last element)

Example 3: Handling Out-of-Bounds Indices – When the index is out of bounds, Array.at() returns undefined.

let arr = [10, 20, 30, 40, 50];

console.log(arr.at(6));     // Output: undefined
console.log(fruits.at(-8)); // Output: undefined

Example 4: The at() method works seamlessly with strings, allowing you to access characters by positive or negative index.

let str = 'hello';

console.log(str.at(0));  // Output: 'h'
console.log(str.at(-1)); // Output: 'o'

Example 5: This example shows how Array.at() simplifies accessing the first or last element of an array.

let arr = [10, 20, 30, 40, 50];

// Access the last element
let lastElement = arr.at(-1);
console.log(lastElement); // Output: 50

// Access the first element
let firstElement = arr.at(0);
console.log(firstElement); // Output: 10

Example 6: Comparison with Bracket Notation

let arr = [10, 20, 30, 40, 50];

console.log(arr[arr.length - 1]); // Output: 50 (Traditional way)
console.log(arr.at(-1));          // Output: 50 (Using at())

Supported Browsers

BrowserSupport
Chrome92+
Firefox90+
Safari15.4+
Edge92+
Opera78+
Internet ExplorerNot supported
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array concat() Method
Next Post: JavaScript Array every() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme