Home >Web Front-end >JS Tutorial >js gets the last element of the array_javascript tips

js gets the last element of the array_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:04:043248browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn