Home >Web Front-end >JS Tutorial >js gets the last element of the array_javascript tips
How to get the last element of an array in js? Here are two methods summarized, friends in need can take a look.
(1) js built-in pop method
The pop() method is used to delete and return the last element of the array. Note that while the last element of the array is obtained, the last element of the original array is also deleted. If the array is already empty, this method does not change the array and returns an undefined value, such as:
<script> var args=new Array(['www'],['jb51'],['net']); alert(args.pop());//net </script>
(2) Obtained according to the length method, for example:
<script> var args=new Array(['www'],['jb51'],['net']); alert(args[args.length-1]);//net </script>
That’s all