How to get last element of array in Javascript?

By Hardik Savani | April 16, 2016 | | 56646 Viewer | Category : Javascript jQuery


Whenever you require to get last element value then you can use bellow example. I give you three way to get last element value in jquery. So let's see following example:

Example

<script type="text/javascript">


var languages = ["PHP", ".Net", "Java", "Javascript"];


var last_element = languages[languages.length-1];

console.log(last_element);


var last_element2 = languages.pop();

console.log(last_element2);


var last_element3 = languages.slice(-1)[0];

console.log(last_element3);


</script>




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.