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

What is the use of javascript push() method?

青灯夜游
Release: 2023-01-05 16:11:32
Original
3701 people have browsed it

In JavaScript, the push() method can add one or more elements to the end of the array and return the new length; syntax "array.push(element 1, element 2, ..., element n )". The push() method directly modifies the array and changes the length of the array, rather than creating a new array.

What is the use of javascript push() method?

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

JavaScript push() method

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

Note: New elements will be added at the end of the array.

Note: This method changes the length of the array.

Syntax:

array.push(item1, item2, ..., itemN)
Copy after login
ParametersDescription
item1, item2, ..., itemNRequired. The element to add to the array.

Return value: new length of array

Example:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.push("James") + "<br />")
document.write(arr)
</script>
Copy after login

Output:

George,John,Thomas
4
George,John,Thomas,James
Copy after login

[Recommended learning: javascript advanced tutorial]

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