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.

0

8 Answers 8

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

Documentation

Sign up to request clarification or add additional context in comments.

6 Comments

@KevinB Yeah, even better
How about checking if the array is empty first?
@RobinSalih The question was "How to select the last thing". An empty array does not have a "last thing". In fact, the implementation should always check this.
If the array is empty, you'll correctly get undefined. I don't see a problem with that.
|
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]; 

Comments

1

Try this:

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

Check this MDN.

Comments

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

1 Comment

Also, this is a duplicate: stackoverflow.com/questions/3216013/…
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.

Comments

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(); 

1 Comment

Extending native prototypes is bad practice according to mdn: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… You don't have to when you do: last = lastInArray.call(arr,arr); still have you method use this
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); 

3 Comments

Given the question didn't indicate anything about changing the contents of myarray, this answer really isn't correct.
Getting ("selecting") should not cause side effects.
@admdrew sorry, my bad, misread question
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]; 

?

5 Comments

so perhaps someone else can tell me if I'm right or wrong What happens when you try this yourself? :)
This will return undefined as -1 is not a valid index of a JavaScript array.
Hmm, just tested it. I get 'undefined'. I really thought that was possible.
negative index works only in jquery: $(array).get(-1);
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. stackoverflow.com/questions/13618571/….

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.