Arrays are **zero-indexed**. Access with `arr[i]`. ES2022 added **arr.at(i)** which supports **negative indexing** (`arr.at(-1)` = last element). Accessing out-of-bounds returns `undefined` (no error). **Destructuring** provides a clean way to extract multiple elements.
1Understanding Array Element Access
Arrays are zero-indexed. Access with arr[i]. ES2022 added arr.at(i) which supports negative indexing (arr.at(-1) = last element). Accessing out-of-bounds returns undefined (no error). Destructuring provides a clean way to extract multiple elements.
Use arr.at(-1) instead of arr[arr.length - 1] to get the last element — it's much cleaner.
const colors = ['red', 'green', 'blue', 'yellow'];
console.log(colors[0]); // 'red'
console.log(colors[2]); // 'blue'
console.log(colors.at(-1)); // 'yellow' (last)
console.log(colors.at(-2)); // 'blue' (second to last)
console.log(colors[99]); // undefined (no error)2Practical Example
Here is a real-world application of Array Element Access showing how it is used in production JavaScript code.
// Destructuring
const [first, second, ...rest] = ['a', 'b', 'c', 'd'];
console.log(first); // 'a'
console.log(second); // 'b'
console.log(rest); // ['c', 'd']3Best Practices
Follow these guidelines when working with Array Element Access:
1. Use at() for negative index access
2. Destructure to extract multiple elements
3. Check array length before accessing computed indices
Tip: Use arr.at(-1) instead of arr[arr.length - 1] to get the last element — it's much cleaner.
const colors = ['red', 'green', 'blue', 'yellow'];
console.log(colors[0]); // 'red'
console.log(colors[2]); // 'blue'
console.log(colors.at(-1)); // 'yellow' (last)
console.log(colors.at(-2)); // 'blue' (second to last)
console.log(colors[99]); // undefined (no error)