Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active August 29, 2015 14:25
Show Gist options
  • Save AllThingsSmitty/6f08712f9f683643beda to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/6f08712f9f683643beda to your computer and use it in GitHub Desktop.
Get last element of an array in one line with no temp vars
// var last = arr.reverse()[0];
Array.prototype.last = function () { return this.length > 0 ? this[this.length -1] : undefined };
@AllThingsSmitty
Copy link
Author

Bad for performance?

@AllThingsSmitty
Copy link
Author

var last = arr[arr.length - 1];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment