Last active
August 29, 2015 14:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// var last = arr.reverse()[0]; | |
Array.prototype.last = function () { return this.length > 0 ? this[this.length -1] : undefined }; |
var last = arr[arr.length - 1];
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bad for performance?