Javascript Array last(n)
/*//from ww w.j a va 2 s . co m Return last n set of elements in an array */ Array.prototype.last = function (n) { 'use strict'; return this.slice(this.length - (n || 1), this.length); };
// Returns the last element of an array. // Passing n will return the last n elements of the array. var array = [5, 4, 3, 2, 1]; Array.prototype._last = function(n){ n = n || 1;/*from w ww . jav a 2 s . c o m*/ return this.slice(-n); }; array._last(); // [1] array._last(3); // [3, 2, 1]