Thanks Tapas Adhikary for writing this. It was very helpful to know about this method. I am definitely going to use it instead of the conventional methods of accessing array elements.
As you had the follow-up questions on Twitter about it, I thought of answering them here to document everything at the same place. Thanks a lot for asking.
I have an input box to accept the index as a number. In the last two marked lines, I am collecting the number from the input box when someone keyup, or change in it. It will take care of both when someone types in the input box or use the up-down slider to change the number.
Once the number is collected in a variable, I pass the number to a method called findAtIndex(index).
Inside the findAtIndex(index) method, I am first checking if the input in NaN. It can happen when you make the input box empty. If it is NaN, I am printing a blank output. Else, I am using the at(index) method to access it using the index.
The number input box and triggering events to access the element using at() was purely for demo purposes.
As the at(index) method is in the proposal now and not added to the JavaScript language yet, we can only use it with the help of a Polyfill.
As the Polyfil I am using is available as an NPM, I have installed it and imported it for using the at() method.
The beauty of it is, if there is support of it in JavaScript language in the future, you can just remove that import from the code along with the polyfill entry from the package.json file. That's it.
I have mentioned the polyfill above under the section, "The at(index) method is Brand New".