How To Concatenate Two Different Arrays In JavaScript

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

In Java script we can merge one or more arrays into one array using concat() method.concat() method merge two different array and make it new array so today we are going to discuss how to concatenate more arrays in java script

How To Concatenate Two Different Arrays In JavaScript

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"];

2.Implement code to merge different array in java script
var Nature = Animals.concat(birds,Tree);
document.getElementById("demo").innerHTML = Nature;


Complete code for merging arrays in java script
<!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>

Related Post