Javascript

3 Methods To Get The Last Element Of An Array In Javascript

Posted by Weston Ganger on June 07, 2016

Need an easy way to get the last element of an array in javascript? Heres a few options:


var myArray = [1,2,3,4,5,6];

// Fastest method, requires the array is in a variable
myArray[myArray.length - 1];

// Also very fast but it will remove the element from the array also, this may or may not matter in your case.
myArray.pop();

// Slowest but very readable and doesn't require a variable
myArray.slice(-1)[0]


Related External Links:


Posted in Javascript and Tagged with javascript 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts