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.