I wrote the code below in an attempt to add an element to the beginning of a new array and return that new array. It works fine, but when I return arr instead of newArr, the arr has changed as well.
function addToFrontOfNew(arr, element) {
newArr = arr;
newArr.unshift(element);
return newArr;
}
If I were to write:
function addToFrontOfNew(arr, element) {
newArr = arr;
newArr.unshift(element);
return arr;
}
and test the function with addToFrontOfNew([1,2], 3) the function would return [3, 1, 2].
How can I rewrite the function so that the original arr is not modified along with the newArr?