js gets the last element of the array

  • 2020-05-27 04:24:38
  • OfStack

How do I get the last element of an array in js? Here is a summary of the two methods, a friend in need can see.

(1) js built-in pop method

The pop() method is used to delete and return the last element of the array. Notice that the last element of the original array is deleted as well as the last element of the array. If the array is already empty, the method does not change the array and returns the undefined value, as follows:


<script>
var args=new Array(['www'],['jb51'],['net']);
alert(args.pop());//net
</script>

(2) according to the length method, for example:


<script>
var args=new Array(['www'],['jb51'],['net']);
alert(args[args.length-1]);//net
</script>

The above is


Related articles: