JS Array Pop & Push

Array Pop()

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

The pop() method changes the original array.

The pop() method returns the removed element.

Syntax

array.pop()???????

Parameters

None

Return Value

Type

Description

A variable

The removed item.
A string, a number, an array, or any other type allowed in an array.

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Sanjay", "Aman", "Rehman", "Karan"];

    document.write(a + "<br><br>");
    a.pop();
    document.write(a);
  </script>
</head>
<body>

</body>
</html>

Array push()

The push() method adds new items to the end of an array.

The push() method changes the length of the array.

The push() method returns the new length.

Syntax

array.push(item1, item2, ..., itemX)

Parameters

Parameters

Description

item1
item2
..
itemX

The item(s) to add to the array.
Minimum one item is required.

Return Value

Type

Description

A number

The new length of the array.

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Sanjay", "Aman", "Rehman"];

    a.push("Rahul");
    document.write(a + "<br><br>");
    a.push("Salman");
    document.write(a + "<br><br>");
  </script>
</head>
<body>
</body>
</html>