136

I have an array and simply want to get the element at index 1.

var myValues = new Array();
var valueAtIndex1 = myValues.getValue(1); // (something like this)

How can I get the value at the 1st index of my array in JavaScript?

aimorris
  • 436
  • 3
  • 18
junaidp
  • 10,801
  • 29
  • 89
  • 137

8 Answers8

211

You can access an element at a specific index using the bracket notation accessor.

var valueAtIndex1 = myValues[1];

On newer browsers/JavaScript engines (see browser compatibility here), you can also use the .at() method on arrays.

var valueAtIndex1 = myValues.at(1);

On positive indexes, both methods work the same (the first one being more common). Array.prototype.at() however allows you to access elements starting from the end of the array by passing a negative number. Passing -1 will give the last element of the array, passing -2 the second last, etc.

See more details at the MDN documentation.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
45

Array indexes in JavaScript start at zero for the first item, so try this:

var firstArrayItem = myValues[0]

Of course, if you actually want the second item in the array at index 1, then it's myValues[1].

See Accessing array elements for more info.

Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
15

You can just use []:

var valueAtIndex1 = myValues[1];
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
11

indexer (array[index]) is the most frequent use. An alternative is at array method:

const cart = ['apple', 'banana', 'pear'];
cart.at(0) // 'apple'
cart.at(2) // 'pear'

If you come from another programming language, maybe it looks more familiar.

nCardot
  • 5,992
  • 6
  • 47
  • 83
Daniel Delgado
  • 4,813
  • 5
  • 40
  • 48
  • Looks like the at method is very new/currently a proposal. It works in Chrome DevTools but not Edabit. https://github.com/tc39/proposal-relative-indexing-method – nCardot Sep 28 '21 at 16:55
2

Update 2022

With ES2022 you can use Array.prototype.at():

const myValues = [1, 2, 3]
myValues.at(1) // 2

at() also supports negative index, which returns an element from the end of the array:

const myValues = [1, 2, 3]
myValues.at(-1) // 3
myValues.at(-2) // 2

Read more: MDN, JavascriptTutorial, Specifications

Lars Flieger
  • 2,421
  • 1
  • 12
  • 34
1

shift can be used in places where you want to get the first element (index=0) of an array and chain with other array methods.

example:

const comps = [{}, {}, {}]
const specComp = comps
                  .map(fn1)
                  .filter(fn2)
                  .shift()

Remember shift mutates the array, which is very different from accessing via an indexer.

Subrat
  • 881
  • 1
  • 11
  • 19
0

You can use [];

var indexValue = Index[1];
Zain
  • 21
  • 3
0

As you specifically want to get the element at index 1. You can also achieve this by using Array destructuring from ES6.

const arr = [1, 2, 3, 4];
const [zeroIndex, firstIndex, ...remaining] = arr;
console.log(firstIndex); // 2

Or, As per ES2022. You can also use Array.at()

const arr = [1, 2, 3, 4];
console.log(arr.at(1)); // 2
Debug Diva
  • 26,058
  • 13
  • 70
  • 123