The concat()
method is used to join two or more arrays.
This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.
Syntax and Usage
array1.concat(array2, array3, ..., arrayX)
Parameter Values
array2, array3, ..., arrayX Required. The arrays to be joined
Let's start
Example(1)
1.Create array
var Animals = ["Zebra", "Squirrel","Loin", "Yak","Rhinoceros"];
var birds = ["Parrots", "Peacock", "Pigeon "];
var Tree = ["Banyan"];
var Nature = Animals.concat(birds,Tree);
document.getElementById("demo").innerHTML = Nature;
<!DOCTYPE html>
<html>
<head>
<title>How To Concatenate Two Different Arrays In JavaScript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>Concatenate Different Arrays In JavaScript</h1>
</div>
<br>
<div class="well">
<button class="btn btn-primary" onclick="lists()">Click it</button>
<h1 id="demo"></h1>
</div>
<br>
</div>
</body>
</html>
<script>
function lists() {
var Animals = ["Zebra", "Squirrel","Loin", "Yak","Rhinoceros"];
var birds = ["Parrots", "Peacock", "Pigeon "];
var Tree = ["Banyan"];
var Nature = Animals.concat(birds,Tree);
document.getElementById("demo").innerHTML = Nature;
}
</script>