Javascript Get Array First Element Example

Apr 08, 2021 . Admin

Hello Friends, Now let's see example of how to get javascript get array first element. We will use how to get if find array first element in javascript. Here you will learn how to use javascript array to get first element of array. This is a short guide on get array first element. Let's get started with how to get first element of array in javascript. Here i will give you many example how you can get javascript first element of array. Example: 1
<!DOCTYPE html>
<html>
<head>
   <title>Javascript - How to check if array first element?-MyWebtuts.com</title>
</head>
<body>

	<script>

		var arr = [ 1, 2, 3, 4, 5 ];
		var first = arr.slice(0,1).shift();
		alert(first);

	</script>

</body>
</html>
Output: 1
	1
Example: 2 In this example i will use attribute for onclick event.
<!DOCTYPE html>
<html>
<head>
	<title>Javascript - How to check if array first element?-MyWebtuts.com</title>
</head>
<body>

	<script>

		var arr = ['php', 'laravel', 'java', 'cs', 'cf' ];
		const [first] = arr;
		console.log(first);
		
	</script>

</body>
</html>
Output: 2
	php

It will help you....

#Javascript