How to Get last Element of Array in Node JS?
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
- How to Install Node Js and NPM Ubuntu?
- How to used Google ReCaptcha V3 in Node Js Express?
- How to Send Email with Attachment in Node js?
- How to Insert Data from Form into MySQL Database using Node js Express?
- Install Node Js and NPM Ubuntu 18.04/20.04/22.04 Tutorial
- How to Delete Data by id into MongoDB in Node JS?
- Install Node.js 16/17/18 on CentOS 8 Tutorial
- How to Upload File in MongoDB using Node js Express?