I am a little confused about the way JavaScript treats objects passed as arguments to functions.
For example, in the following code:
var a = {
val: "old"
};
var b = {
val: "old"
};
function update(a, b) {
a.val = "new";
b = {
val: "new"
};
}
update(a, b);
console.log(a, b);
The output comes as:
The val
property of a
is changed but that of b
is not. I read somewhere that Objects are passed by reference to functions. Can anyone please explain this behaviour.
Thanks in advance.