Home  >  Article  >  Web Front-end  >  What is the use of javascript push() method?

What is the use of javascript push() method?

青灯夜游
青灯夜游Original
2021-04-26 14:52:303644browse

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)
Parameters Description
item1, item2, ..., itemN Required. 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>

Output:

George,John,Thomas
4
George,John,Thomas,James

[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!

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