Using JavaScript’s .at() method
Blog post from LogRocket
The JavaScript .at() method offers developers an efficient way to access elements in arrays and strings using index values, including the ability to use negative indices to retrieve items from the end of a list. Unlike traditional methods such as bracket notation, length property, or slice(), the .at() method simplifies the syntax for accessing elements and supports decimal index values by considering only the integer part. This makes it particularly useful for tasks like random selection in applications, such as a rock-paper-scissors game. The method is widely supported by modern browsers and provides a more concise alternative to preexisting techniques, enhancing the overall developer experience by reducing complexity and eliminating the need for additional steps like using Math.floor for random index selection.