I was working on a bug in some code that I wrote, and the culprit turned out to be this snippet (simplified for the sake of the question):
var a = [1, 2, 3, 5, 4];
// later...
function f(arr) {
var sorted = arr.sort();
// other stuff
}
// later...
console.log(a);
// logs [1, 2, 3, 4, 5]!
My question is, why does a
stay sorted after the function is done? Surely in JS all variables are passed by value as opposed to reference?