Skip to main content
added 222 characters in body
Source Link
Aram Kocharyan
  • 20.5k
  • 11
  • 84
  • 98

A shorter version of what @chaiguy posted:

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

Reading the -1 index returns undefined already.

EDIT:

These days the preference seems to be using modules and to avoid touching the prototype or using a global namespace.

export function last(array) { return array[array.length - 1]; } 

A shorter version of what @chaiguy posted:

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

Reading the -1 index returns undefined already.

A shorter version of what @chaiguy posted:

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

Reading the -1 index returns undefined already.

EDIT:

These days the preference seems to be using modules and to avoid touching the prototype or using a global namespace.

export function last(array) { return array[array.length - 1]; } 
Source Link
Aram Kocharyan
  • 20.5k
  • 11
  • 84
  • 98

A shorter version of what @chaiguy posted:

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

Reading the -1 index returns undefined already.