Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - JOIN TWO ARRAYS


How to extend an existing Array or join two Arrays?


Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to insert a new Array with two names Sia and Andrew at the end of the Array.


There are two ways by which we can join or extend Array.

  1. By using the + operator

  2. Using the extend() Function

At first let us see the Joining of two Arrays using + operator.


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Salim"]
	var y = ["Sia", "Andrew"]
	var z = x.concat(y)
	document.write(z)
    
</script>      
</body>
</html>


Output :



  Mohan,Kriti,Salim,Sia,Andrew

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Also we have another Array that contains, Sia and Andrew.


	y = ["Sia", "Andrew"]
java_Collections


Next, we have used the concat() function to add the the Arrays x and y.


var z = x.concat(y)

And the Arrays x and yis joined and initialised to a new Array z.

java_Collections

And we get the below output,


Mohan,Kriti,Salim,Sia,Andrew