I have a question about Array.at()
method that was introduced Array.prototype.at() - MDN. It seems more useful than squared brackets annotation arr[i]
since there is an option to specify index starting from the end of an array, for example: arr.at(-1)
which would return the last member of arr
array. Also, it seems more appropriate for method chaining. Are there any downsides to switching to it?
To replace an element in an array: Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.
Introduction to the JavaScript array indexOf() methodTo find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.
JavaScript arrays are zero-indexed: the first element of an array is at index 0 , the second is at index 1 , and so on — and the last element is at the value of the array's length property minus 1 . JavaScript array-copy operations create shallow copies.
Array.prototype.at()The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
A couple of disadvantages to it are:
.at
may confuse some of them (and cause them to have to look it up). Not that this means that you shouldn't use it, but it's something to keep in mind.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With