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

Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript

WBOY
Release: 2022-08-04 10:37:11
Original
1657 people have browsed it

This article brings you relevant knowledge about javascript, which mainly introduces the use of JavaScript’s push(), pop(), unshift(), and shift() methods, as follows Let's take a look, I hope it will be helpful to everyone.

Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript

[Related recommendations: javascript video tutorial, web front-end]

Summary introduction:

pop(): This method is used to delete the last element of the array and return the deleted element.

Official syntax:

arrayObject.pop()
Copy after login

shift(): This method is used to delete the first element of the array and return the deleted element.

Official syntax:

arrayObject.shift()
Copy after login

push(): This method is used to add one or more elements to the end of the array and return the new length.

Official syntax:

arrayObject.push(newelement1,newelement2,....,newelementX)
Copy after login

unshift(): This method is used to add one or more elements to the beginning of the array and return the new length.

Official syntax:

arrayObject.unshift(newelement1,newelement2,....,newelementX)
Copy after login

1. push() method

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

Syntax:

arrayObject.push(newelement1,newelement2,....,newelementX)
Copy after login

Parameter description:

newelement1: Required. The first element to be added to the array.

newelement2: Optional. The second element to be added to the array.

newelementX: Optional. Multiple elements can be added.

Return value:

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

Instance:

<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:

Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript

##2. pop() method

The pop() method removes and returns the last element of an array.

Syntax:

arrayObject.pop()
Copy after login

Return value:

The last element of arrayObject.

Example:

<script type="text/javascript">

    var arr = new Array(3)
    arr[0] = "George"
    arr[1] = "John"
    arr[2] = "Thomas"

    document.write(arr)
    document.write("<br />")
    document.write(arr.pop())
    document.write("<br />")
    document.write(arr)

</script>
Copy after login

Output:

Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript

##3, unshift()

unshift The () method adds one or more elements to the beginning of the array and returns the new length.

Syntax:

arrayObject.unshift(newelement1,newelement2,....,newelementX)
Copy after login

Parameter description:

    newelement1: Required. The first element added to the array.
  • newelement2: Optional. The second element added to the array.
  • newelementX: Optional. Several elements can be added.
  • Return value:

The new length of arrayObject.

Example:

<script type="text/javascript">

    var arr = new Array()
    arr[0] = "George"
    arr[1] = "John"
    arr[2] = "Thomas"

    document.write(arr + "<br />")
    document.write(arr.unshift("William") + "<br />")
    document.write(arr)

</script>
Copy after login

Output:

Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript##4, shift()

shift The () method is used to remove the first element from the array and return the value of the first element.

Syntax:

arrayObject.shift()
Copy after login

Return value:

The value of the original first element of the array.

Instance:

<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.shift() + "<br />")
    document.write(arr)

</script>
Copy after login
Output:

[Related recommendations: Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScriptjavascript video tutorial

,

web front end

The above is the detailed content of Summarize and organize the push(), pop(), unshift(), and shift() methods in JavaScript. 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