*"but it should print null"* - No it shouldn't. The global variable `a` is never re-assigned to `null`. The **local** variable `a` is, within the scope of the function where it's declared, but then that variable immediately falls out of scope and is never observed. You might see the difference more clearly by using a different name for the local variable within the function.
– DavidJul 11 '23 at 12:14
the parameter variable is not a reference of the global. see https://stackoverflow.com/questions/5373278/what-is-the-correct-term-for-variable-shadowing-in-javascript?rq=1
– evolutionxboxJul 11 '23 at 12:18
@shreyakjain: No, giving a local variable the same name as a global variable doesn't make it a reference to that global variable. It *shadows* the global variable. (Which means the global variable can no longer be accessed in that scope, because there's no way to differentiate it from the local variable.) Give different *names* to different variables and the code will become more clear.
– DavidJul 11 '23 at 12:20
2
`a` contains the reference to the object as value. `a.a = 10;` manipulates the object by reference. `a = null;` assigns a new value. `a` is not a reference. It contains a reference. It would be easier if you were using different variable names for the global variable and the local variable. `a` is a copy of `a`.
– jabaaJul 11 '23 at 12:21