How to Get last Element of Array in Node JS?

13-Aug-2022

.

Admin

Hello Friends,

Now, let's see an example of the get last Element of Array in Node Js Example. you will learn Node.js to get the last element of the array. I explained simply how to get the last element of an array in node.js?. We will look at the example of get the last item in the array in Node Js. follow bellow step for array get the last element get using Node Js.

In this example, I will create a simple array with the days with the name. Then I will get the last element of the array using length() function and key.

let's see below a simple example with output:

Example: 1


index.js

myArray = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

# Get Last Element

lstElem = myArray[6];

console.log(lastElem);

Output:

Sat

Example: 2

index.js

myArray = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

# Get Last Element

lastArray = myArray[myArray.length - 1];

console.log(lastArray);

Output:

Sat

I hope it can help you...

#Node JS