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

What is the use of push method in JavaScript?

青灯夜游
Release: 2023-01-07 11:41:15
Original
5506 people have browsed it

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)
Copy after login
ParametersDescription
item1, item2, ..., itemXRequired. 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
Copy after login

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!

Related labels:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template