Home > Web Front-end > JS Tutorial > body text

js gets the last element of the array_javascript tips

WBOY
Release: 2016-05-16 16:04:04
Original
3223 people have browsed it

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>
Copy after login

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

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

That’s all

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template