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

What does the javascript push() method do?

藏色散人
Release: 2023-01-07 11:46:20
Original
2835 people have browsed it

The function of the javascript push() method is to add one or more elements to the end of the array and return the new length. Its usage syntax is "array.push(item1, item2, ..., itemX) ", where parameter item1 represents the element to be added to the array.

What does the javascript push() method do?

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

What does the javascript push() method do?

javascript push() method

The push() method adds one or more elements to the end of the array and returns the new length.

The syntax is:

array.push(item1, item2, ..., itemX)
Copy after login

Parameters item1, item2, ..., itemX are required. The element to add to the array.

Return value: Number New array length

Example:

Add more than one element

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi","Lemon","Pineapple")
Copy after login

Output

Banana,Orange,Apple,Mango,Kiwi,Lemon,Pineapple
Copy after login

Note: New element will be added at the end of the array. This method changes the length of the array.

Tip: To add elements at the beginning of the array, please use the unshift() method.

Recommended study: "javascript basic tutorial"

The above is the detailed content of What does the javascript push() method do?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!