The at() method is a modern, read-only way to grab a single array element by index—including negative indexes that count from the end. This tutorial covers syntax, five worked examples, comparisons with bracket notation, and pitfalls every beginner should know.
01
Syntax
array.at(index)
02
Forward
at(0), at(2)
03
Backward
at(-1) last item
04
Safe reads
Out-of-range → undefined
05
vs []
Negative index gap
06
Support
ES2022 / modern runtimes
Fundamentals
Introduction
Arrays are the backbone of data manipulation in JavaScript. For years, developers used bracket notation—arr[0], arr[arr.length - 1]—to read elements. The at() method (added in ES2022) offers a cleaner alternative that understands negative indexes counting from the tail.
In this guide you will learn how at() works, when to prefer it over brackets, and how to use it safely in real programs.
Concept
Understanding the at() Method
at() is a simple accessor: pass an index, get back the element at that position. Positive indexes walk forward from the start; negative indexes walk backward from the end. The method never changes the array—it only reads.
Choose at() when you want readable relative indexing (especially at(-1) for the last item) or when the index comes from a variable and may fall outside the array bounds.
💡
Beginner Tip
Think of at(-1) as “one step back from the end.” It replaces the old pattern array[array.length - 1] with something you can read at a glance.
Foundation
📝 Syntax
General form of Array.prototype.at:
JavaScript
array.at(index)
Parameters
array — the array (or typed array) you read from.
index — zero-based position; negative values count backward from length.
Return value
The element at the resolved index.
undefined when the index is out of range or the slot is empty (sparse array hole).
Common patterns
arr.at(0) — first element (same as arr[0] on dense arrays).
arr.at(-1) — last element without length - 1 math.
arr.at(i) inside a loop — dynamic index from a counter or user input.
Cheat Sheet
⚡ Quick Reference
Goal
Code
First element
arr.at(0)
Last element
arr.at(-1)
Second-to-last
arr.at(-2)
Out of range
Returns undefined
Mutates array?
No
Compare
📋 at() vs Bracket Notation
Four common ways to read a single slot. The cards below show when at() shines—especially for negative offsets—and where bracket syntax still applies or misleads.
Forward
at(2)
Third element
Backward
at(-1)
Last element
Bracket trap
arr[-1]
Not the last item!
Legacy
arr[arr.length - 1]
Pre-ES2022 pattern
Hands-On
Examples Gallery
Open DevTools Console (F12) and paste each snippet, or use the Try-it links. Every example uses a sample languages array.
📚 Getting Started
Read elements at fixed forward positions.
Example 1 — Access Elements at Positive Indexes
Retrieve the first and third programming languages using at(0) and at(2).
languages[-1] looks up the string property "-1" on the array object, which does not exist, so the result is undefined. Only at() treats -1 as a relative offset.
Example 4 — Safe Access with Bounds Checking
When the index is dynamic, combine at() with an explicit check for clearer error messages.
JavaScript
const languages = ["JavaScript", "Python", "Java", "C++", "Ruby"];
function pickLanguage(index) {
const value = languages.at(index);
if (value === undefined) {
return "Index out of bounds.";
}
return "Language at " + index + ": " + value;
}
console.log(pickLanguage(2)); // Language at 2: Java
console.log(pickLanguage(5)); // Index out of bounds.
at(5) on a five-element array resolves beyond the last valid index and returns undefined. Your helper can turn that into a friendly message instead of silently passing undefined downstream.
Example 5 — Selective Access Inside a Loop
Read only even-indexed elements while iterating.
JavaScript
const languages = ["JavaScript", "Python", "Java", "C++", "Ruby"];
for (let i = 0; i < languages.length; i++) {
if (i % 2 === 0) {
const currentLanguage = languages.at(i);
console.log("Language at even index " + i + ": " + currentLanguage);
}
}
Language at even index 0: JavaScript
Language at even index 2: Java
Language at even index 4: Ruby
How It Works
Inside the loop, at(i) and languages[i] behave the same for valid indexes. Using at keeps the style consistent when other branches use negative offsets.
Applications
🚀 Common Use Cases
Last element — items.at(-1) in logs, queues, and chat histories.
Relative reads — grab the previous item with at(-2) without length math.
Dynamic indexes — user-selected positions or pagination offsets passed as variables.
Conditional loops — read specific slots while filtering by index parity or pattern.
Strings & typed arrays — same API on "hello".at(-1) and Uint8Array.
Readable refactors — replace arr[arr.length - 1] with arr.at(-1).
🧠 How at() Resolves an Index
1
Convert index
If index < 0, add array.length to get the forward position.
Normalize
2
Range check
If the result is < 0 or ≥ length, return undefined.
Guard
3
Return element
Otherwise return the value at the resolved integer index.
Read
4
📦
No mutation
The original array is untouched—only a reference is returned.
Important
📝 Notes
at() is read-only; use splice, with, or assignment for changes.
Non-integer indexes are converted with ToIntegerOrInfinity before resolving.
Sparse arrays: a hole at a valid index still yields undefined.
Falsy elements (0, "", false) are valid return values—do not confuse them with “missing.”
Feature-detect with if ("at" in Array.prototype) before relying on it in legacy bundles.
String.at and TypedArray.at share the same negative-index semantics.
Compatibility
Browser & Runtime Support
Array.prototype.at() is part of ES2022. All evergreen browsers shipped it in 2021–2022; Node.js added it in version 16.6.
✓ Baseline · ES2022
Array.prototype.at()
Supported in Chrome 92+, Firefox 90+, Safari 15.4+, Edge 92+, and Node 16.6+. Not available in Internet Explorer.
95%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.at()Excellent
Bottom line: Safe for modern web apps and current Node LTS releases. For IE11 or very old embedded browsers, feature-detect with if ("at" in Array.prototype) and fall back to arr[arr.length - 1] or a small polyfill.
Wrap Up
Conclusion
The at() method streamlines array element access in modern JavaScript. It keeps forward reads as simple as brackets while adding intuitive backward indexing through negative numbers.
Practice the examples above until arr.at(-1) feels natural—it is one of the most practical ES2022 additions for everyday array work.
Verify browser/Node support or polyfill for old targets
Keep indexes as integers when possible
Use the same style across arrays, strings, and typed arrays
❌ Don’t
Assume array[-1] returns the last item
Treat every undefined result as “out of range” (could be a stored value or hole)
Use at when you need a sub-array—use slice instead
Forget that at requires ES2022 support
Expect at to mutate the array
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.at()
Your foundation for modern index access in JavaScript.
5
Core concepts
📝01
Syntax
array.at(index)
API
🔢02
Negative n
at(-1) = last.
Tail read
🗃03
Safe OOB
Returns undefined.
No throw
🔄04
Read-only
Never mutates.
Accessor
⚠05
Not [-1]
Brackets differ.
Pitfall
❓ Frequently Asked Questions
Array.prototype.at(index) returns the element at a given position. Positive indexes count from the start (0 is first); negative indexes count from the end (-1 is last). It does not mutate the array.
For non-negative indexes they usually match on dense arrays. The big difference is negative indexing: array.at(-1) returns the last element, but array[-1] is undefined in JavaScript because -1 is treated as a string property key, not a relative offset.
It returns undefined when the resolved index is before 0 or at/after length—the same as reading a missing slot with bracket notation, but without throwing an error.
Yes. String.prototype.at and TypedArray.prototype.at follow the same negative-index rules for characters and numeric slots.
at() is part of ES2022. Modern browsers and Node 16.6+ support it. For very old environments, use a polyfill or fall back to array[array.length - 1] for the last element.
at() never throws for bad indexes—it returns undefined. You only need an explicit check when your app logic must distinguish 'missing element' from a valid undefined stored in the array.
Did you know?
Unlike bracket notation, array.at(-1) reads the last element without writing array[array.length - 1]. The same negative-index idea also exists on String.prototype.at and typed arrays.