Skip to content

Instantly share code, notes, and snippets.

@hitecherik-gist
Created January 2, 2013 14:58
Show Gist options
  • Save hitecherik-gist/4435113 to your computer and use it in GitHub Desktop.
Save hitecherik-gist/4435113 to your computer and use it in GitHub Desktop.
JS: returnLast() (returns last in array)
/**
* Created by Alexander Nielsen. alex@hitecherik.site11.com
* 2 January 2013
*/
/**
* Returns the last item in an array
* @param {Array} arr Array to return last of
* @return {var} The last item in the array
*/
function returnLast(arr){
var lastIndex = arr.length - 1;
return arr[lastIndex];
}
// example of use
var myArray = [1, 2, 10, 400, 50, 98, 43]; //array of 7 integers
var lastItem = returnLast(myArray); //in this case 43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment