JavaScript concat() Method : Array Object
Description
The concat() method is used to join two or more arrays. Concat does not alter the original arrays, it returns a copy of the same elements combined from the original arrays.
Version
Implemented in JavaScript 1.2
Syntax
concat(arrayname1, arrayname2, ..., arraynameN)
Parameter
arrayname1, arrayname2, ..., arraynameN : Arrays to be joined to an array.
Example -1:
In the following web document two arrays arrayname1 and arrayname2 with single element have joined by concat() method.
HTML Code
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>JavaScript concat() method example on two arrays</title>
<meta name="description" content="This document contains an example of JavaScript Array constructor Property" />
<style type="text/css">
h1{color: red;}
</style>
</head>
<body>
<h1>JavaScript : concat() method</h1>
<script src="array-concat-example1.js"></script>
</body>
</html>
JS Code
var arrayname1 = new Array("USA");
var arrayname2 = new Array("INDIA");
var newarray = arrayname1.concat(arrayname2);
var newParagraph = document.createElement("p");
var newText = document.createTextNode(newarray);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
View the example in the browser
Practice the example online
JavaScript concat() method example on two arraysExample -2:
In the following web document three arrays array1, array2 and array3 with multiple elements have joined by concat() method.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript concat() method example on multiple arrays</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript : concat() method</h1>
<script src="array-concat-example2.js"></script>
</body>
</html>
JS Code
var array1=new Array(2);
array1[0] = "Fruits";
array1[1] = "Color";
var array2=new Array(2);
array2[0] = "Orange";
array2[1] = "Apple";
var array3=new Array(2);
array3[0] = "Banana";
array3[1] = "Cherry";
var joinarray=array1.concat(array2,array3);
var newParagraph = document.createElement("p");
var newText = document.createTextNode(joinarray);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
View the example in the browser
Practice the example online
JavaScript concat() method example on multiple arraysSee also:
JavaScript Core objects, methods, properties.
Previous: JavaScript prototype Property: Array Object
Next:
JavaScript join() Method: Array Object
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics