JavaScript Array at() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Array access

What You’ll Learn

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

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.

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.

📝 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.

⚡ Quick Reference

GoalCode
First elementarr.at(0)
Last elementarr.at(-1)
Second-to-lastarr.at(-2)
Out of rangeReturns undefined
Mutates array?No

📋 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

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).

JavaScript
const languages = ["JavaScript", "Python", "Java", "C++", "Ruby"];

const firstLanguage = languages.at(0);
const thirdLanguage = languages.at(2);

console.log(firstLanguage);  // "JavaScript"
console.log(thirdLanguage);  // "Java"
Try It Yourself

How It Works

Index 0 points to the first slot; index 2 skips two positions and returns "Java". On dense arrays this matches languages[0] and languages[2].

📈 Practical Patterns

Negative offsets, comparisons, and dynamic access.

Example 2 — Navigate from the End (Negative Index)

Use at(-1) to read the last element and at(-2) for the penultimate one.

JavaScript
const languages = ["JavaScript", "Python", "Java", "C++", "Ruby"];

const lastLanguage = languages.at(-1);
const penultimate = languages.at(-2);

console.log(lastLanguage);   // "Ruby"
console.log(penultimate);    // "C++"
Try It Yourself

How It Works

Negative indexes resolve relative to length. For an array of length 5, at(-1) maps to index 4 (the last slot).

Example 3 — at(-1) vs array[-1]

Bracket notation does not support Python-style negative indexing in JavaScript.

JavaScript
const languages = ["JavaScript", "Python", "Java", "C++", "Ruby"];

console.log(languages.at(-1));  // "Ruby"
console.log(languages[-1]);     // undefined

// Classic alternative before at() existed:
console.log(languages[languages.length - 1]);  // "Ruby"
Try It Yourself

How It Works

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.
Try It Yourself

How It Works

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);
  }
}
Try It Yourself

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.

🚀 Common Use Cases

  • Last elementitems.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.

📝 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

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.

💡 Best Practices

✅ Do

  • Use at(-1) for the last element
  • Check for undefined when indexes are dynamic
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.at()

Your foundation for modern index access in JavaScript.

5
Core concepts
🔢 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.

Continue to concat()

Learn how to combine multiple arrays into one without mutating the originals.

concat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful