/

HTML / CSS / JavaScript Tutorial

JavaScript Array method: push()

[this page | pdf | back links]

The push() method (when applied to a JavaScript array) joins arrays and returns a copy of the joined array.

 

It has the following syntax with the following parameters. It returns a number representing the new length of the array.

 

array.push(item1, item2, …)

 

Parameter

Required / Optional

Description

item1, item2, …

Required

The item(s) to be added to the array

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var h = [-1, 2, 3];
var y = h.push(5, 4);
document.getElementById("Example").innerHTML =
  'var h = [-1, 2, 3];<br>' +
  'var y = h.push(5, 4);<br>' +
  'var x = h + "|" + y;';
document.getElementById("Result").innerHTML = h + "|" + y;
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodArrayPush() {
  var h = [1, 2, 3]; return !!h.push;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Arrays


Desktop view | Switch to Mobile