An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
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
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.
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
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To