How to get last key in array in javascript?

arrays, javascript

Solution

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

The preferred term is element, rather than key.

EDIT: Or do you mean the last index? That is `myarray.length - 1`. Keep in mind, JavaScript arrays can only be indexed numerically (though arrays are also objects, which causes some confusion).

Problem

This is similar to this question,only PHP->javascript How to get numeric key of new pushed item in PHP?

Original source

Related problems