Home >Web Front-end >Front-end Q&A >What is the use of push method in JavaScript?
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.
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:
【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!