The Wayback Machine - https://web.archive.org/web/20241122150410/https://www.geeksforgeeks.org/javascript-array-concat-method/
Open In App

JavaScript Array concat() Method

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The concat() method concatenates (joins) two or more arrays. It returns a new array, containing the joined arrays. This method is useful for combining arrays without modifying the originals.

Syntax:

let newArray1 = oldArray.concat()
let newArray2 = oldArray.concat(value0)
let newArray3 = oldArray.concat(value0,value1)
.......
.......
let newArray = oldArray.concat(value1 , [ value2, [ ...,[ valueN]]])

Parameters:

The parameters of this method are the arrays or the values that need to be added to the given array. The number of arguments to this method depends upon the number of arrays or values to be merged.

Return value:

This method returns a newly created array that is created after merging all the arrays passed to the method as arguments. 

Example 1: Below is an example of the Array concat() method to join three arrays.

JavaScript
// JavaScript code for concat() method
function func() {
    let num1 = [11, 12, 13],
        num2 = [14, 15, 16],
        num3 = [17, 18, 19];
    console.log(num1.concat(num2, num3));
}
func();

Output
[
  11, 12, 13, 14, 15,
  16, 17, 18, 19
]

Example 2: In this example, the method concat() concatenates all the arguments passed to the method with the given array into one array which it returns as the answer.

JavaScript
// JavaScript code for concat() method
function func() {
    let alpha = ["a", "b", "c"];
    console.log(alpha.concat(1, [2, 3]));
}
func();

Output
[ 'a', 'b', 'c', 1, 2, 3 ]

Example 3: In this example, the method concat() concatenates both arrays into one array which it returns as the answer.

JavaScript
// JavaScript code for concat() method
function func() {
    let num1 = [[23]];
    let num2 = [89, [67]];
    console.log(num1.concat(num2));
}
func();

Output
[ [ 23 ], 89, [ 67 ] ]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

The browsers supported by the JavaScript Array concat() method are listed below:

  • Chrome 51
  • Edge 15
  • Firefox 54
  • Safari 10
  • Opera 38

JavaScript Array concat() Method – FAQs

What does the Array.prototype.concat() method do in JavaScript?

The Array.prototype.concat() method is used to merge two or more arrays. It returns a new array containing the elements of the original arrays, in order.

Can concat() be used to add individual elements to an array?

Yes, you can use concat() to add individual elements as well as arrays to an existing array. For example: array1.concat(element1, element2).

How does concat() handle non-array arguments?

If non-array arguments are passed to concat(), they are added as individual elements to the new array.

Can concat() handle nested arrays?

Yes, concat() can handle nested arrays, but it does not flatten them. Nested arrays remain nested in the resulting array.

What are common use cases for the concat() method?

  • Merging Arrays: Combining multiple arrays into a single array.
  • Adding Elements: Adding individual elements to an array without modifying the original array.
  • Creating Copies: Creating shallow copies of arrays by concatenating an empty array.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Previous Article
Next Article

Similar Reads

JavaScript String concat() Method
The concat() method in JavaScript join or concatenate two or more strings together. It does not change the existing strings and returns a new string. This method efficiently combines multiple strings into one, ensuring the original strings remain unchanged. Syntaxstr1.concat(str2, str3, str4,......, strN)Parametersstr1, str2, ..., strN: This method
3 min read
TypeScript Array concat() Method
The TypeScript Array.concat() method merges two or more arrays into a new array. It takes multiple arrays and/or values as parameters and returns a single array containing all the elements. This method helps efficiently combine data from various sources into one array. Syntaxarray.concat(value1, value2, ..., valueN)Parameter: This method accepts a
2 min read
Node.js Buffer.concat() Method
The Buffer.concat() method is used to concat all buffer objects in a given array into one buffer object. The return value of this method is also a buffer. If the length of the buffer is not provided then it is calculated from the Buffer instances in the list. Syntax: Buffer.concat( list, length ) Parameters: This method accepts two parameters as me
2 min read
Collect.js concat() Method
The concat() method is used to return the merged arrays or objects represented by the collection. The JavaScript array is first transformed into a collection and then the function is applied to the collection. Syntax: collect(array1).concat(array2) Parameters: The collect() method takes one argument that is converted into the collection and then co
2 min read
Ember.js Ember.Templates.helpers concat() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
2 min read
TypeScript String concat() Method
The concat() method in TypeScript merges two or more strings into one new string. It takes multiple string arguments and concatenates them in the order provided, returning the combined result. Syntaxstring.concat(string2, string3[, ..., stringN]); Parameter: This method accepts a single parameter as mentioned above and described below. string2...st
2 min read
Difference Between Spread Operator and Array.concat() in Typescript
The spread operator and concat() method both are used to add elements to an array. But they are different from each other in many ways. Let us discuss the difference between both of them in detail. Spread OperatorThe spread operator creates a new array by merging the elements of the passed arrays or the values. It is denoted by three dots(...). It
3 min read
Tensorflow.js tf.concat() Function
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. The tf.concat() function is used to concatenate the list of specified Tensors along the given axis. Syntax: tf.concat (tensors, axis)Parameters: This function accepts two parameters wh
2 min read
p5.js concat() function
The concat() function in p5.js is used to concatenate the two given arrays. This function is depreciated from future versions and uses First_array.concat(Second_array) instead. Syntax: concat(First_array, Second_array) Parameters: This function accepts two parameters as mentioned above and described below: First_array: The first array will be conca
1 min read
How to concat strings using AngularJS ?
In this article, we will see how to concat strings in AngularJS. There are few ways to concat the strings in AngularJS. In this article, we will see 2 of them. Example 1: In the first example, we are using the '+' operator to concat the strings [GFGTABS] HTML <!DOCTYPE HTML> <html> <head> <script src= "https://ajax.googlea
2 min read
Lodash _.concat() Function
Lodash _.concat() function is used to concatenate the arrays in JavaScript. and it returns the new concatenated array. Syntax:_.concat(array, [values]);Parameters:array: It is an array to which values are to be added.values: It is the Array of values that is to be added to the original array.Note: The array value can also contain arrays of an array
3 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery
3 min read
How does Promise.all() method differs from Promise.allSettled() method in JavaScript ?
In this article, we will first understand in brief the Promise.all() as well as Promise.allSettled() methods and then we will try to visualize how they differ from each other through some theoretical as well as some coding examples. Both Promise.all() and Promise.allSettled() methods are the methods of a Promise object (which is further a JavaScrip
4 min read
How does Promise.any() method differs from Promise.race() method in JavaScript ?
In this article, we will first try to understand how we may declare or use Promise.any() and Promise.race() methods followed by some facts which will eventually help us to understand how they differ from each other (through theoretical as well as coding examples). Let us first quickly understand in brief about both the methods followed by their syn
5 min read
How toReversed() method is different from reverse() method in JavaScript
In JavaScript, reverse() and toReversed() methods do the same job as reversing but toReversed() does not change the original array. The reverse() method changes the original array. Below we have explained both methods. Table of Content Reverse() MethodtoReversed() methodReverse() MethodThe reverse() method is a built-in method for arrays in JavaScr
2 min read
Implement polyfill for Array.prototype.map() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or because the new version of the br
3 min read
JavaScript Array valueOf() Method
The JavaScript Array valueOf() method in JavaScript is used to return the array. It is a default method of the Array Object. This method returns all the items in the same array. It will not change the original content of the array. It does not contain any parameter values. Syntax: array.valueOf()Parameters: This method does not accept any parameter
2 min read
Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read
Which built-in method removes the last element from an array and returns that element in JavaScript ?
In this article, we will know how to remove the last element from an array using the built-in method in Javascript, along with understanding its implementation through the examples. There are a few Javascript built-in methods and techniques with which we can remove the last element of the array. We will focus only on the 2 methods ie., the pop() me
3 min read
Implement polyfill for Array.prototype.reduce() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.reduce() method in JavaScript. A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or the new version of the browser does not have that
2 min read
JavaScript Array findLastIndex() Method
The Javascript findLastIndex() method is used to find the index of the last element in an array that matches the specified condition. It works by iterating over the array from the end to the beginning and returning the index of the first element that satisfies the condition specified in a callback function. If no element matches the condition, the
2 min read
How the Array.unshift() method works in JavaScript ?
The Array.unshift() method in JavaScript is used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array after the elements have been added. Key points about Array.unshift(): It modifies the original array and returns the new length.It can accept multiple arguments, and each a
1 min read
What is the use of the Array.from() method in JavaScript ?
In JavaScript, the Array.from() method is used to create a new array instance from an iterable object or array-like object. It provides a way to convert objects that are not inherently arrays into actual arrays. Example: Here we see that output creates a new array whose content is the same as input in the case of an integer. C/C++ Code console.log(
1 min read
What is the use of the Array.some() method in JavaScript ?
The Array.some() method in JavaScript is used to check if at least one element in an array satisfies a given condition. It returns true if at least one element in the array passes the test implemented by the provided function otherwise, it returns false. Example: Here, the condition in the some() method checks if there is at least one element great
1 min read
What is the use of the Array.of() method in JavaScript ?
The JavaScript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method. Example: Here, Array.of() method is used to create new array instances with a variable number of arguments. C/C++ Code // Here the Array.of() method creates a new Array instance with // a variab
1 min read
What is Array.prototype.slice() method in JavaScript ?
The Array.prototype.slice() method in JavaScript is used to extract a portion of an array and create a new array containing the selected elements. It does not modify the original array. instead, it returns a shallow copy of a portion of the array. Syntax:array.slice(startIndex, endIndex);Parameters:startIndex: The index at which to begin extraction
1 min read
What is the use of the Array.findIndex() method in JavaScript ?
The Array.findIndex() method in JavaScript is used to find the index of the first element in an array that satisfies a provided testing function. It returns the index of the first element for which the testing function returns true. If no such element is found, it returns -1. Syntax:array.findIndex(function(currentValue, index, arr), thisValue);Par
2 min read
What is the use of the Array.sort() method in JavaScript ?
The Array.sort() method is used to sort the elements of an array in place and returns the sorted array. Example: Here, the sort() method is called on an array of numbers. By default, sort() sorts elements as strings, so it sorts the numbers based on their Unicode code points. As a result, the numbers are sorted in ascending order. The sorted array
1 min read
What is the use of the Array.reduceRight() method in JavaScript ?
The Array.reduceRight() method in JavaScript is used to reduce the elements of an array from right to left into a single value. It iterates over the array in reverse order, applying a callback function to each element and accumulating a result. This method is similar to Array.reduce(), but it starts the iteration from the last element of the array
2 min read
What is the use of the Array.shift() method in JavaScript?
The Array.shift() method in JavaScript is used to remove the first element from an array and return that removed element. This method modifies the original array by removing the first element and shifting all subsequent elements to a lower index. Syntax:array.shift()Example: Here, the shift() method is called on the array containing [1, 2, 3, 4, 5]
1 min read
three90RightbarBannerImg