freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../functional-programming/add-elements-to-the-end-of-.../index.md

801 B

title
Add Elements to the End of an Array Using concat Instead of push

Add Elements to the End of an Array Using concat Instead of push

Where the push method adds new element to the end of the orginal array, the concat method creates a new array containing the elements from the original array and the new element. The original array remains the same when using concat.

Solution

function nonMutatingPush(original, newItem) {

  // Add your code below this line
  
  return original.concat(newItem);

  // Add your code above this line
}

var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);