3
var myarray = [];

var result1 = myarray[0];
var result2 = myarray[1];
var result3 ....etc...

Selecting first is easy, cause it's just 0. How do I put the last one into a var? It seems like such a simple question but I must have been googling wrong cause I could not find any answer that worked.

Shonna
  • 993
  • 2
  • 9
  • 12

8 Answers8

9
var last = myarray[myarray.length - 1];

Documentation

Tobias
  • 7,723
  • 1
  • 27
  • 44
3

myarray.length returns the length of array and as the elements have started from 0 subtracting one from it will give the index of last element

var lastresult = myarray[myarray.length - 1];
Vikas Sangle
  • 632
  • 5
  • 19
1

Try this:

var l= myarray[myarray.length - 1];

Check this MDN.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1
var lastelement = myarray[myarray.length - 1]
  • Also, this is a duplicate: http://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array –  Dec 09 '13 at 20:19
1

One way would be to do something like this

var lastElement = myArray[myarray.length-1]

Don't overthink. Arrays may be 0 indexed in javascript but that just means that the last index has to be the length of the array -1.

ford prefect
  • 7,096
  • 11
  • 56
  • 83
1

Just this:

var lastResult = myarray[myarray.length - 1];

You can create prototype to do this more simple. For example:

if (!Array.prototype.last){
  Array.prototype.last = function(){
    return this[this.length - 1];
  };
};

and than use

myarray.last();
BaBL86
  • 2,602
  • 1
  • 14
  • 13
  • Extending native prototypes is bad practice according to mdn: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain#Bad_practice.3A_Extension_of_native_prototypes You don't have to when you do: `last = lastInArray.call(arr,arr);` still have you method use `this` – HMR Dec 10 '13 at 02:13
1

you could use array prototype method .pop() but that will remove last item from array:

var lastElement = myarray.pop();

Or wrap array in jq object:

var lastElement = $(myarray).get(-1);
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

I could be wrong about this, so perhaps someone else can tell me if I'm right or wrong, but can't you just do:

var last = myArray[-1]

?

EDIT: I'm wrong. Ignore me.

Also, out of curiosity, would it be faster to use

var lastelement = myarray[myarray.length - 1];

or

var lastelement = myarray.reverse[0];

?

awesame
  • 84
  • 2
  • 13
  • 3
    `so perhaps someone else can tell me if I'm right or wrong` What happens when you try this yourself? :) – admdrew Dec 09 '13 at 20:27
  • 1
    This will return `undefined` as `-1` is not a valid index of a JavaScript array. – Tobias Dec 09 '13 at 20:29
  • Hmm, just tested it. I get 'undefined'. I really thought that was possible. – awesame Dec 09 '13 at 20:34
  • negative index works only in jquery: `$(array).get(-1);` – A. Wolff Dec 09 '13 at 20:36
  • Yeah. I did a little more digging on this. Turns out that negative indices can be used to assign properties to an array object, but they do not affect length. This thread does a good job of explaining it. http://stackoverflow.com/questions/13618571/should-negative-indexes-in-javascript-arrays-contribute-to-array-length. – awesame Dec 09 '13 at 20:40