Home>Article>Web Front-end> What is the use of push method in JavaScript?

What is the use of push method in JavaScript?

青灯夜游
青灯夜游 Original
2021-06-28 16:45:10 5320browse

In JavaScript, the push() method is used to add one or more elements to the end of an array and return the length of the array after adding the elements; the syntax format is "array.push(element 1, element 2, . .., element X)", this method must have at least one parameter.

What is the use of push method in JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript push() method

push() method can add the elements specified by the parameters to the end of the array in turn, and return the array length after adding the elements (the Method must have at least one parameter).

Grammar format:

array.push(item1, item2, ..., itemX)
Parameters Description
item1,item2, ...,itemX Required. The element to add to the array.

Return value: new length of array

Example is as follows:

var arr = [1,2,3]; console.log(arr.push(4));//返回最终数组的长度:4 console.log(arr);//返回:1,2,3,4 console.log(arr.push(5,6,7));//返回最终数组的长度:7 console.log(arr);//返回:1,2,3,4,5,6,7

Output:

What is the use of push method in JavaScript?

【Related recommendations:javascript learning tutorial

The above is the detailed content of What is the use of push method in JavaScript?. 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