inspection
Array.prototype.at
Returns the element at the given integer index, supporting negative values that count from the end (ES2022). Reach for it as the clean replacement for `arr[arr.length - 1]`.
arr.at(index) Parameters
| Parameter | Purpose |
|---|---|
| index | Integer index; negative values count from the end (-1 is last) |
| return value | The element at that index, or undefined if out of range |
Examples
console.log(['a','b','c'].at(0)); Logs: 'a'
console.log(['a','b','c'].at(-1)); Logs: 'c'
console.log(['a','b','c'].at(-2)); Logs: 'b'
console.log(['a','b','c'].at(99)); Logs: undefined
Gotcha
Requires ES2022 (Node 16.6+, all modern browsers). Bracket notation `arr[-1]` returns undefined — always use `.at(-1)` for the last element.
Related methods
Array.prototype.slice
Returns a shallow copy of a portion of the array from start (inclusive) to end (exclusive) without modifying the source. Reach for it to extract a range or to clone an array (`arr.slice()`).
Array.prototype.indexOf
Returns the first index where searchElement is strictly equal (===) to an array item, or -1. Reach for it for a fast, exact primitive lookup — but not for objects or NaN.
Array.prototype.includes
Returns true if the array contains searchElement using SameValueZero equality (ES2016). Reach for it as the readable boolean alternative to `indexOf(x) !== -1`.
Array.prototype.concat
Returns a new array formed by joining the source with the provided arrays and/or values (one level of spread). Reach for it to merge arrays without mutating any of them.
Array.prototype.join
Returns a string formed by concatenating each element, separated by the given string (default ','). Reach for it to build CSV/URL/paths or any delimited string from an array.