/

HTML / CSS / JavaScript Tutorial

JavaScript Array method: concat()

[this page | pdf | back links]

The concat() 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 an array.

 

array.concat(array1, array2, …)

 

Parameter

Required / Optional

Description

array1, array2, …

Required

The arrays to be joined (inserted after the original array in the returned result)

 

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 = ["a","b"];
document.getElementById("Example").innerHTML =
  'var h = ["a","b"];' + '<br>' +
  'var x = h.concat(["c","d"],"e");';
document.getElementById("Result").innerHTML
  = h.concat(["c","d"],"e");
</script>

</body>
</html>

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


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Arrays


Desktop view | Switch to Mobile