Why i can't reassign a different value to a array inside a function passed as parameter in javascript?

Viewed 21

When there is a array defined at global level, and when i pass that array to a function and then reassign a new value, this not reflect in the global array. It should work right? because arrays are passed by reference.

let arr = [1, 2, 3, 4];

function reassign(array) {
    array = [5, 6, 7, 8]
    console.log(array); //[ 5, 6, 7, 8 ]
}

reassign(arr)

console.log(arr); //[ 1, 2, 3, 4 ]

0 Answers
Related