Getting an item an array with a negative index with modern JavaScript

Today, we’re going to look at the Array.prototype.at() method: what it does, how it works, and why you might want to use it over simple bracket notation.
Let’s dig in!
Getting an array item by its index The Array.prototype.at() method gets an array item at a specific index. Pass the index in as an argument.
Let’s imagine you have an array of wizards…
let wizards = [‘Merlin’, ‘Ursula’, ‘Ragadast’, ‘Gandalf’]; You can use the Array.


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to look at the Array.prototype.at() method: what it does, how it works, and why you might want to use it over simple bracket notation.

Let’s dig in!

Getting an array item by its index

The Array.prototype.at() method gets an array item at a specific index. Pass the index in as an argument.

Let’s imagine you have an array of wizards

let wizards = ['Merlin', 'Ursula', 'Ragadast', 'Gandalf'];

You can use the Array.prototype.at() method to get the item at index 2 like this…

// returns "Ragadast"
let radagast = wizards.at(2);

Why would you use this over bracket notation?

JavaScript already provides a way to get items by their index: bracket notation.

// returns "Ursula"
let ursula = wizards[1];

So… why would you use the Array.prototype.at() method instead of bracket notation?

The Array.prototype.at() method has one advantage over bracket notation: it supports negative indexes.

If you used an index of -1 with bracket notation, you get undefined as the returned value.

// returns undefined
let maybeGandalf = wizards[-1];

But with the Array.prototype.at() method, negative indexes are counted backwards from the end of the array. An index of -1 returns Gandalf instead.

let gandalf = wizards.at(-1);

Level-up your team’s JavaScript skills. I offer custom training programs tailored to your needs with a mix of self-paced courses, live workshops, and mentoring.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-05-16T14:30:00+00:00) Getting an item an array with a negative index with modern JavaScript. Retrieved from https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/

MLA
" » Getting an item an array with a negative index with modern JavaScript." Go Make Things | Sciencx - Tuesday May 16, 2023, https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/
HARVARD
Go Make Things | Sciencx Tuesday May 16, 2023 » Getting an item an array with a negative index with modern JavaScript., viewed ,<https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/>
VANCOUVER
Go Make Things | Sciencx - » Getting an item an array with a negative index with modern JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/
CHICAGO
" » Getting an item an array with a negative index with modern JavaScript." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/
IEEE
" » Getting an item an array with a negative index with modern JavaScript." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/. [Accessed: ]
rf:citation
» Getting an item an array with a negative index with modern JavaScript | Go Make Things | Sciencx | https://www.scien.cx/2023/05/16/getting-an-item-an-array-with-a-negative-index-with-modern-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.