Categories
JavaScript Answers

How to get value at a specific index of array in JavaScript?

Spread the love

Sometimes, we want to get value at a specific index of array in JavaScript.

In this article, we’ll look at how to get value at a specific index of array in JavaScript.

How to get value at a specific index of array in JavaScript?

To get value at a specific index of array in JavaScript, we can use square brackets or the at method.

For instance, we write

const valueAtIndex1 = myValues[1];

to get the value at index 1 from the myValues array with square brackets.

We can do the same thing by calling at by writing

const valueAtIndex1 = myValues.at(1);

We can call at with negative indexes.

Negative indexes starts with -1 for the last item in an array.

Conclusion

To get value at a specific index of array in JavaScript, we can use square brackets or the at method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *