mergo two arrays in javascript

To merge two arrays in javascript, there are multiple ways to achieve that:

  1. Using concat() method:

    The concat() method is used to merge two or more arrays into a new array.

    index.tsx
    const arr1 = [1, 2, 3];
    const arr2 = [4, 5, 6];
    
    const mergedArray = arr1.concat(arr2);
    console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]
    
    144 chars
    6 lines
  2. Using spread operator (...) :

    We can use the spread operator (...) to merge two arrays into a new array.

    index.tsx
    const arr1 = [1, 2, 3];
    const arr2 = [4, 5, 6];
    
    const mergedArray = [...arr1, ...arr2];
    console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]
    
    145 chars
    6 lines

Both of these methods will result in a new array that contains all the elements from both arrays.

gistlibby LogSnag