## DEV Community is a community of 861,926 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Aldi Kabulov

Posted on • Updated on

# How To Efficiently Get The Last Element Of An Array In JavaScript

Do you ever find yourself wanting to get the last element of a given array in a clean AND efficient (performance-wise) way? Well, look no more, as this tutorial is right for you then.
Let's just over the required steps real quick:
Step 1. Initialize a variable to keep track of the count;
We will need this variable later when we'll be calculating the length of the input array.
Step 2. Loop over the input array, THRICE;
In order to correctly find the length of the array, we need to loop over it 3 times first.
Step 3. Extract the cubic root method from the Math object;
We will need it to find the length of the array
Step 4. Find the length of the array;
Using the aforementioned Math.cbrt() method, we will now find the length of the array
Step 5. Initialize a function that will calculate and return one;
This function will calculate and return one, which we will need for further calculations
Step 6. Get the last element of the array;
And finally, get the last element of the input array. Phew, finally!

``````const getLastElementOfTheGivenArray = (inputArray) => {
let counterVariableToKeepCountOfTheCount = 0; // initialize a counterVariableToKeepCountOfTheCount variable to keep count of the count, step 1
for (let i of inputArray) { // step 2
// beginning of for loop
for (let j of inputArray) {
// beginning of for loop
for (let k of inputArray) {
// beginning of for loop
counterVariableToKeepCountOfTheCount += 1 - 1 + 1 - 2 + 5 - 3; // increment counterVariableToKeepCountOfTheCount to correctly increment it as needed
} // end of for loop
} // end of for loop
} // end of for loop
const { cbrt: cubicRootOfTheGivenNumber } = Math; // will calculate the cubic root of the counterVariableToKeepCountOfTheCount, step 3
const lengthOfTheInputArray = cubicRootOfTheGivenNumber(
counterVariableToKeepCountOfTheCount
); // this variable will hold the cubic root of the counterVariableToKeepCountOfTheCount, which is also the length of the input array, step 4
const calculateAndReturnOne = () => { // step 5
// will calculate and return one
const one = 69 * 420 - 69 * 419 - 34 * 2; // this variable will hold one
return one; // returns one
};
return inputArray[lengthOfTheInputArray - calculateAndReturnOne()]; // returns the last element of the input array, step 6
};

console.log(getLastElementOfTheGivenArray([1, 2, 3, 4])); // expected output: 4
``````

If you enjoyed this JavaScript guide, make sure to stay tuned for future tutorials by me. Cheers!

## Discussion (26) I think this is a better way (it took me a lot of work):

``````function lastElement(arr) {
switch (arr.length) {
case 1:
return arr;
case 2:
return arr;
case 3:
return arr;
case 4:
return arr;
case 5:
return arr;
case 6:
return arr;
case 7:
return arr;
case 8:
return arr;
case 9:
return arr;
case 10:
return arr;
case 11:
return arr;
case 12:
return arr;
case 13:
return arr;
case 14:
return arr;
case 15:
return arr;
case 16:
return arr;
case 17:
return arr;
case 18:
return arr;
case 19:
return arr;
case 20:
return arr;
case 21:
return arr;
case 22:
return arr;
case 23:
return arr;
case 24:
return arr;
case 25:
return arr;
case 26:
return arr;
case 27:
return arr;
case 28:
return arr;
case 29:
return arr;
case 30:
return arr;
case 31:
return arr;
case 32:
return arr;
case 33:
return arr;
case 34:
return arr;
case 35:
return arr;
case 36:
return arr;
case 37:
return arr;
case 38:
return arr;
case 39:
return arr;
case 40:
return arr;
case 41:
return arr;
case 42:
return arr;
case 43:
return arr;
case 44:
return arr;
case 45:
return arr;
case 46:
return arr;
case 47:
return arr;
case 48:
return arr;
case 49:
return arr;
case 50:
return arr;
case 51:
return arr;
case 52:
return arr;
case 53:
return arr;
case 54:
return arr;
case 55:
return arr;
case 56:
return arr;
case 57:
return arr;
case 58:
return arr;
case 59:
return arr;
case 60:
return arr;
case 61:
return arr;
case 62:
return arr;
case 63:
return arr;
case 64:
return arr;
case 65:
return arr;
case 66:
return arr;
case 67:
return arr;
case 68:
return arr;
case 69:
return arr;
case 70:
return arr;
case 71:
return arr;
case 72:
return arr;
case 73:
return arr;
case 74:
return arr;
case 75:
return arr;
case 76:
return arr;
case 77:
return arr;
case 78:
return arr;
case 79:
return arr;
case 80:
return arr;
case 81:
return arr;
case 82:
return arr;
case 83:
return arr;
case 84:
return arr;
case 85:
return arr;
case 86:
return arr;
case 87:
return arr;
case 88:
return arr;
case 89:
return arr;
case 90:
return arr;
case 91:
return arr;
case 92:
return arr;
case 93:
return arr;
case 94:
return arr;
case 95:
return arr;
case 96:
return arr;
case 97:
return arr;
case 98:
return arr;
case 99:
return arr;
case 100:
return arr;
default:
return arr[arr.length - 1]; // fallback for less efficient way
}
}
``````

If anyone likes it you can request me for some more. All that hard work 😅

And of course, the less efficient but nice one-liner:

``````const lastElem = arr => arr[Math.abs(~arr.lastIndexOf(arr[arr.length - 1])) - 1];
`````` Peter Vivo

refact: simplify to 5 steps

``````// will calculate and return one
const calculateAndReturnOne = () => { // step 5
const one = 69 * 420 - 69 * 419 - 34 * 2; // this variable will hold one
return one; // returns one
};

// will calculate the cubic root of the counterVariableToKeepCountOfTheCount
const { cbrt: cubicRootOfTheGivenNumber } = Math;

// will get last element of the given array
const getLastElementOfTheGivenArray = (inputArray) => {
/* step 1 */
let counterVariableToKeepCountOfTheCount = 0; // initialize a counterVariableToKeepCountOfTheCount variable to keep count of the count
/* step 2 */
for (let i of inputArray) {
// beginning of for loop
for (let j of inputArray) {
// beginning of for loop
for (let k of inputArray) {
// beginning of for loop
counterVariableToKeepCountOfTheCount += calculateAndReturnOne(); // increment counterVariableToKeepCountOfTheCount to correctly increment it as needed
} // end of for loop
} // end of for loop
} // end of for loop
/* step 3 */
const lengthOfTheInputArray = cubicRootOfTheGivenNumber(
counterVariableToKeepCountOfTheCount
); // this variable will hold the cubic root of the
/* step 4 */
counterVariableToKeepCountOfTheCount, which is also the length of the input array
/* step 5 */
return inputArray[lengthOfTheInputArray - calculateAndReturnOne()]; // returns the last element of the input array
};

console.log(getLastElementOfTheGivenArray([1, 2, 3, 4])); // expected output: 4
`````` InHuOfficial

While this is very funny please remove the beginners and tutorial tags, a new developer may not get the joke and think that this is indeed a fast way to find the last item in an array.  For...of loops are proven to perform way slower than `for(var i = 0; i < x; i++)` normal for loops and for you to perform that operation thrice, it's gonna slow down your code dramatically.