Home  >  Article  >  Web Front-end  >  What does the javascript push() method do?

What does the javascript push() method do?

藏色散人
藏色散人Original
2021-09-01 14:14:362816browse

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)

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")

Output

Banana,Orange,Apple,Mango,Kiwi,Lemon,Pineapple

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!

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
Previous article:What does nodejs do?Next article:What does nodejs do?