I feel like I'm almost grasping it... like it intuitively makes sense but I don't know the details of why. Can someone explain:
Why here, global arr is NOT affected outside the function
arr = [1,2,3]
function test(arr, a, b) {
arr = arr.filter(item => (a < item && item < b))
console.log(arr) // [2]
}
test(1, 3)
console.log(arr) // [1,2,3]
Why here, global arr IS affected outside the function
arr = [1,2,3]
function test(a, b) {
arr = arr.filter(item => (a < item && item < b))
console.log(arr) // [2]
}
test(1, 3)
console.log(arr) // [2]
EDIT another example. Why here global arr IS affected outside the function EVEN THOUGH once again arr is passed as a parameter
arr = [1,2,3]
function test(arr, a, b) {
for (let i = 0; i < arr.length; i++) {
let val = arr[i];
if (a >= val || val >= b) {
arr.splice(i, 1);
i--;
}
}
console.log(arr) // [2]
}
console.log(arr) // [2]