Array concat() Method
The Array concat()
method returns a merged array consisting of the array on which it is called upon and the array(s) provided as its arguments in order.
The syntax for using this method is as
array.concat(array1, .. , arrayN);
Consider the array ["Debian", "Fedora"], on which the concat()
method will be applied. We will pass the array ["openSUSE", "Ubuntu"] as an argument to the concat()
method
> ["Debian", "Fedora"].concat(["openSUSE", "Ubuntu"]);
The output will be a new array of comprising of the array on which the concat()
method is applied and the array ["openSUSE", "Ubuntu"] passed as its argument
["Debian", "Fedora", "openSUSE", "Ubuntu"]
We next pass another array ["Gentoo"] as the concat()
method's second argument
> ["Debian", "Fedora"].concat(["openSUSE", "Ubuntu"], ["Gentoo"]);
and it gives a new array of joined elements of the arguments in order
["Debian", "Fedora", "openSUSE", "Ubuntu", "Gentoo"]
However, the argument(s) need not always be an array. An string value can also be passed to the concat()
method as an argument, and it will be joined as an element of the newly created array in order
> ["Debian", "Fedora"].concat(["openSUSE", "Ubuntu"], "Gentoo");