ES2022 introduced the array method “at()“.
Example
Retrieve the third element of “fruits” using the “at()” method.
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; let fruit = fruits.at(2); |
Access the third element of “fruits” using square brackets [].
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; let fruit = fruits[2]; |
The “at()” method retrieves an indexed element from an array.
It yields the same result as using square brackets [].
This method is supported in all modern browsers since March 2022.
NOTE: In various programming languages, negative bracket indexing, such as [-1], permits accessing elements from the end of an object, array, or string. However, in JavaScript, this feature isn’t available because square brackets [], used for accessing both arrays and objects, don’t support negative indexing. Instead, obj[-1] refers to the value of the key -1, not the last property of the object. To resolve this issue, the at() method was introduced in ES2022. |
The join() method, similar to toString(), combines all array elements into a string. However, it offers the additional functionality of allowing you to specify the separator.
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits.join(” * “); |
Result:
Banana * Orange * Apple * Mango |