Skip to solution
easyPhone Screen

What do Array.prototype.at() and String.prototype.at() do differently from bracket indexing, and why were they added?

278 views
01

Understand the problem

Question presented to candidate: "What does .at() actually add on top of regular bracket indexing for arrays and strings, and why did the language add a whole new method just for this?"

What a strong answer should cover:

  • .at(index) accepts negative integers, counting back from the end: at(-1) is the last element, at(-2) is second-to-last, and so on.
  • Bracket notation does not support negative indices at all -- arr[-1] does not throw, it just looks up the property named "-1" on the array, which does not exist, so it returns undefined.
  • Both a valid negative index and an out-of-range index (too large positive, or too negative) return undefined for .at() with no throw.
  • at() is defined generically across indexable built-ins -- plain arrays, strings, and typed arrays all support it with the same semantics.
  • Motivation: before at(), getting the last element required arr[arr.length - 1], which recomputes/re-reads length and reads awkwardly, especially when arr itself is the result of a function call that would otherwise need to be invoked twice.

Clarifying questions expected:

  • "Should I also cover TypedArray.prototype.at(), or just Array and String?"
  • "Do you want me to compare this against .slice(-1)[0], which was the common pre-at() workaround?"

Code / implementation expected: Yes -- a runnable comparison of at() against bracket indexing for both positive and negative indices, including out-of-range behavior.

arraystringat-method
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed output is re

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSArray.prototype.at() and String.prototype.at() vs bracket indexing (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 32 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track