Object are passed with their reference in javascript. Meaning change in that object from any where should be reflected. In this case, the expected output was {} for console.log(a)
function change(a,b) {
a.x = 'added';
a = b;//assigning a as {} to b
}
a={}
b={}
change(a,b);
console.log(a); //expected {} but output {x:'added'}
console.log(b)
What is happening here? It should not be because of functional scope as far as I know. Thank you
If you added another line you can get a clearer picture of what is happening:
When you're doing
a = byou're assigning the local variableato the reference thatbis holding.