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




concat() - FUNCTION


The concat() Function is used to create a new array by concatenating two or more arrays.


Let us say, we have two array,


["Mohan", "Kriti", "Salim"]

And


["Sia", "Andrew"]

And we want to concatenate them into one Array.


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.


var 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.


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


Next, we have used the extend() function to add the new Array y that contains Sia and Andrew at the end of the Array x.


var z = x.concat(y)

And the Array y is joined with x.

java_Collections

And we get the below output,


Mohan,Kriti,Salim,Sia,Andrew

Now, what if we want to join three arrays.


Let us see in the below example.


Example :



<html>
<body>  
<script>

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


Output :



  Mohan,Kriti,Salim,Sia,Andrew,Sam,Cas