How To Get Last Array Element in Javascript Example

Apr 09, 2021 . Admin

Hello Friends,

Now let's see example of how to get javascript get array last element. Here you will learn how to use javascript array to get last element of array. We will use how to get if find array last element in javascript. This is a short guide on get array last element. Let's get started with how to get last element of array in javascript.

Here i will give you many example how you can get javascript last element of array.

Example: 1
In this example i will use array length for get last element of array.
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if array last element?-MyWebtuts.com</title>
</head>
<body>

	<script>

		let arry = [2, 4, 6, 8, 10, 12, 14, 16];
		let lastElement = arry[arry.length - 1];

		console.log(lastElement);
		
	</script>
</body>
</html>
Output: 1
	16
Example: 2 In this example i will use slice() method for get last element of array.
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if array last element?-MyWebtuts.com</title>
</head>
<body>

	<script>

		let arry = [45, 67, 87, 54, 43, 21, 11];
		let lastElement = arry.slice(-1);

		console.log(lastElement);
		
	</script>
</body>
</html>
Output: 2
	11
It will help you....
#Javascript