DEV Community

loading...

Discussion on: JS interview in 2 minutes / value vs reference

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited

What you described is "call by sharing" (en.wikipedia.org/wiki/Evaluation_s...).

Passing by reference would mean that it is possible to modify the passed variable, such as

function modifyVariable(x) {
    x = {b: 0};
}

let a = {a: 0};
modifyVariable(a);
Enter fullscreen mode Exit fullscreen mode

variable a now should be equal to {b: 0} if it was passed by reference.

Thread Thread
javier123454321 profile image
Javier Gonzalez

I see it's not a direct reference to the variable because you cant reasign that reference, however, you do have access to the internals of the parameter that was passed. Thanks for clearing that up, I have often heard that you pass by reference in javascript but TIL.

Thread Thread
kozlovzxc profile image
Nikita Kozlov Author

Thanks for the detailed answer @lukaszahradnik and thanks for asking for more details @javier123454321 🙏 I have added update notes to the post itself.

Thanks again for clarifying this!