Try Out JavaScript's Array.at() Function!

preview_player
Показать описание


Prepping for your frontend interviews? Use code "conner" for a discount on my course FrontendExpert:

Рекомендации по теме
Комментарии
Автор

The inconsistency of JavaScript:
[1, 2, 3].at(0) = 1 // first item
[1, 2, 3].at(-1) = 3 // last item

Why index starts from zero when left to right but index start from -1 when right to left

why not [1, 2, 3].at(0) = 1 // first item
and [1, 2, 3].at(-0) = 3 // last item

waynes
Автор

Been using this forever on leetcode, displays as an error but it works 😂

nikolatodorovic
Автор

I use at(index) all the time, but didn't know about negative arguments. And I don't like it! It's not right.

waynes
Автор

Thanks for sharing, I found array.length - 1 syntax quite weird coming from python
This is more familiar and concise for coding interviews

_boris