JavaScript Get Last Element of Array

As we know array to store multiple values. So, there are methods to get last element of an array using JavaScript. Also, you saw an example javascript get the last element of an array in the below section.

As an example, we created an array and in array holds multiple values. as well as, array store a variable.

Here, also an example of getting the last and first element in JavaScript. So, you can see in this section impediment get last n elements of the array in JavaScript.

<html>
<head>
<title>This is example of last element of array JavaScript</title>
</head>
<body>

<script>

var animals = ['monkey', 'lion', 'don', 'frog', 'cow'];

var lastobject = animals[animals.length - 1];
	
	document.write('Last Element of array:-'+ lastobject );
	console.log(lastobject );

</script>
</body>
</html>

Output:-

Last Element of an array:- Cow

JavaScript Get Last Element of Array

Thus, you saw above all sections showing an example to an array the last element using JavaScript.

So, define an array with the holds all array values in a variable. the variable name has animals in which array defines multiple values of animals example.

Now, JavaScript provides a method to get the element of an array. Firstly, this method counts all elements in an array.

There is an option to get an element that must get first and last both are possible. Likewise, first starting 0 to n times and last starting variable name and length -1.

var animals = ['monkey', 'lion', 'don', 'frog', 'cow'];
var lastobject = animals[animals.length - 1];	
	document.write('First Element-'+ firstobject);

Get First and Last Element of Array JavaScript

Js array method to get the first and last element. Also, must know about when get the first element to apply this method [0]. Similarly, second, get an element [2].

Here, we provide both methods first and last with the example of code. Firstly, showing an array to get an element.

Also, Store all value of array in a variable. As well as, apply for first [0] using javascript. For last element [-1].

<html>
<head>
<title>First and Last element of array javascript</title>
</head>
<body>

<script>

var student = ['name', 'rollno', 'address', 'contactno', 'subject'];

var firstobject = student[0];
var lastobject = student[student.length - 1];

	
	document.write('First object of array-'+ firstobject);
	document.write('Last object of array-'+ lastobject);
	
</script>

</body>
</html>

Output:-

The First object of an array :- Name

Last object of an array:- Subject

JS Get Element of Array

Similarly, this method has the same but JavaScript in the array gets any value which is consists of an array.

Therefore, array holds multiple values now each value count to starting 0 to n. Then, JavaScript given the main methods to get all and each element from the array using this method.

Firstly, get the third element from the array you can put this method on your script arrayname[3]. Similarly, pick every element the same as Js array.

<script>

var student = ['name', 'rollno', 'address', 'contactno', 'subject'];

var thirdobject = student[3];
	
	document.write('Third Element-'+ thirdobject);
	
	
</script>

Conclusion

Finally, we example all types of method that Js gets the last element of the array. Whenever want another example and any issues regarding it. So, you can ask our comment section.

Also, the main suggestion relative to this topic. which is very helpful to understand some points to follow How to Pass Parameter in JavaScript Function From Html

Leave a Reply

Your email address will not be published. Required fields are marked *

JAVASCRIPT

Recent Posts

Related Post

JavaScript Change Image onclick Event

We are displaying the best method to JavaScript change image onclick event with the example. As well as, given another way to implement ‘change image on button click javascript’. Also, We can do change image on mouse hover and mouse click event in the below section. After that, showing an example of change multiple image […]

Read more

How to fix WordPress jQuery is not defined

So, you are looking at how to fix ‘WordPress jQuery is not defined’. The major issue is some time WordPress ‘uncaught referenceerror: jquery is not defined’ or $ is not defined jQuery. Therefore, when does this error appear on the WordPress website? when jQuery is not defined on your WordPress project. they come to appear […]

Read more

How to Pass Parameter in JavaScript Function From Html

Here, you are looking at the best few examples that how to pass parameter in JavaScript function from Html. As well as, explain in detail what is a function parameter and multiple arguments pass with the onClick event using JavaScript function. For example, you saw an above example of a function with the passing two […]

Read more

x