Syntax, Parameter, and Return Value
The syntax of _.concat() is straightforward, yet it offers ample flexibility:
Syntax
_.concat(array, [values])
Parameters
- array (Array): The initial array to be concatenated.
[values] (...*): Additional values or arrays to be concatenated with the first array. These can be arrays or individual values.
Return Value
Array: Returns a new array consisting of the merged values.
_.concat() does not modify the original arrays; it produces a new array containing elements from the original arrays and the additional values.
Examples of Lodash _.concat() Method
Example 1: Concatenating Multiple Arrays
JavaScript
const _ = require('lodash');
const array1 = [1];
const array2 = [2, 3];
const array3 = [4, 5, 6];
const concatenatedArray = _.concat(array1, array2, array3);
console.log(concatenatedArray);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3, 4, 5, 6]
This example demonstrates merging multiple arrays into a single array.
Example 2: Concatenating Array with Values
JavaScript
const moreValues = _.concat(array1, 2, [3, 4], [[5]]);
console.log(moreValues);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3, 4, [5]]
Here, _.concat() combines an array with individual values and nested arrays.
Example 3: Concatenating with Non-Array Values
JavaScript
const nonArrayValues = _.concat('a', ['b', 'c'], 'd');
console.log(nonArrayValues);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['a', 'b', 'c', 'd']
In this example, strings are concatenated with an array, illustrating the method's versatility with different data types.
Example 4: Concatenating Empty Arrays
JavaScript
const emptyArray1 = [];
const emptyArray2 = [];
const combinedEmpty = _.concat(emptyArray1, emptyArray2);
console.log(combinedEmpty);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[]
Concatenating empty arrays results in an empty array, showcasing the method's consistency.
Frequently Asked Questions
Does _.concat() change the original arrays?
No, _.concat() does not modify the original arrays. It creates a new array containing the combined elements.
Can _.concat() handle deep nested arrays?
_.concat() merges arrays one level deep. For deeper nesting, a different method, like _.flattenDeep(), might be required.
Is _.concat() limited to arrays only?
No, _.concat() can concatenate arrays with individual elements, including numbers, strings, objects, and more.
Conclusion
The _.concat() method in Lodash is a versatile tool for array manipulation in JavaScript. It excels in merging arrays and incorporating additional elements into arrays, all while maintaining the integrity of the original data. Its ease of use and flexibility make it a valuable asset in data handling, contributing to more efficient and readable code.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.