Assume we have a code:
const someNumber = 3;
const someArray = [1, 2, 3];
function changeArray(sampleArray, numberToPush) {
sampleArray.push(numberToPush);
return sampleArray;
}
function incrementNum(number) {
number++;
return number;
}
const modifiedArray = changeArray(someArray, 5);
const incrementedNum = incrementNum(someNumber);
console.log(someNumber, incrementedNum, someArray, modifiedArray, ) // 3, 4, [1, 2, 3, 5], [1, 2, 3, 5]
I don't understand why it is working this way. If we put a number as an argument and increment it, then the original number is not affected. If we put an array as an argument and push some additional number, then the original array is affected.
I know that I can create a copy of an array inside a function, but I don't understand this behavior - why original array as an argument is affected but a number as an argument is not affected?