Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 12, 2021 05:37 am GMT

Concat() Array Method in JavaScript

Can we merge two arrays in one array in JavaScript???
Yes, It's definitely possible by concat() array method.
So, Let's start to learn what is concat() array method and how it works.

What is Concat() Array
method ?
=> In this method two or more
than two arrays merge into
a single array.
The new array serialized
according to original
arrays.

Syntax of Concat() method
array1.concat(array2)

Example

example 1

Explanation :-
(1) In the above example I
simply created two
arrays and name of
arrays are 'arr1' and
'arr2' respectively.

(2) Then I stored some
values in that arrays and
values are [1,2] and
[3,4].

(3) Now I was used cancat()
method to merge both
arrays.

(4) According to above
example I wrote arr1 first
and then used concat()
method and pass as
parameter second array and
the output is [1,2,3,4].

(5) Because doing this it's
return arr1 first then
arr2. If I wrote arr2
first then concat method
used the output is
different. See below code

![example 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otzxwn7tdcfjn8c7eeyz.jpg)

(6) Now, you can see the
output different, output
is [3,4,1,2].

The concat() methods also take individual as arguments like below.

example 3

Happy coding....
Stay tuned with us for more
JavaScript topics.
Thanks for reading...


Original Link: https://dev.to/dev_shubham/concat-array-method-in-javascript-5b58

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To