Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the push () method in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 10:40:46 09月16日 Update

This article mainly introduces the relevant knowledge of "how to use the push() method in JavaScript". Xiaobian shows you the operation process through actual cases. The operation method is simple and fast and practical. I hope this article "how to use the push() method in JavaScript" can help you solve the problem.

The push() method is mainly used to add one or more elements to the end of the array, and its return value is the new length after addition, that is, the length of the array after push, which is of type number.

Push definition and usage

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

syntax

arrayObject.push(newelement1,newelement2,...., newelementX)

parameter description

newelement1 is required. The first element to add to the array.

newelement2 is optional. The second element to add to the array.

newelementX is optional. Multiple elements can be added.

return value

The new length after adding the specified value to the array.

description

The push() method adds its argument order to the end of an arrayObject. It modifies arrayObject directly instead of creating a new array. The push() and pop() methods use the advanced post-pop functionality provided by arrays.

Tips and comments

Note: This method changes the length of the array.

Tip: To add one or more elements to the beginning of an array, use the unshift() method.

Examples of Push

In this example, we will create an array and change its length by adding an element:

vararr=newArray(3)

arr[0]="George"

arr[1]="John"

arr[2]="Thomas"

[xss_clean](arr+"")

[xss_clean](arr.push("James")+"")

[xss_clean](arr)

Output:

George,John,Thomas

4

Gorge,John,Thomas,James

TIY

push()

The content of "how to use push() method in JavaScript" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Tags: Method array element length multiple knowledge parameter end comment industry prompt different practical advanced content function example practicality actual tail Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Shulou Tech Info macOS Microsoft