The at() method returns the element at a given index, supporting negative indices for accessing from the end. Here's how to use it.
Basic Usage
Comparison with Bracket Notation
Working with Strings
TypedArrays
Practical Examples
Array Slicing Patterns
Method Chaining
Safe Access Pattern
Pagination Helper
Ring Buffer
Best Practices
Usage:
✓ Use for negative indexing
✓ Cleaner than length - n
✓ Works on strings too
✓ Safe for method chaining
Benefits:
✓ More readable code
✓ Consistent with other languages
✓ Works with TypedArrays
✓ Returns undefined for out of bounds
Patterns:
✓ Get last element: arr.at(-1)
✓ Get second to last: arr.at(-2)
✓ Safe access with fallback
✓ Method chaining
Avoid:
✗ Using when you need the index
✗ Forgetting undefined return
✗ Overcomplicating simple access
✗ Using with very old browsers
Conclusion
The at() method provides a clean syntax for accessing array elements with negative indices. Use it to get elements from the end without calculating length - n. It works on arrays, strings, and TypedArrays, and returns undefined for out-of-bounds access rather than throwing an error.