DEV Community

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

Collapse
 
smac89 profile image
smac89 • Edited

I don't think pass-by-reference exists in JavaScript. That's a term popularized by C++ or C. In those languages, objects can either be values or reference, therefore you need the pass-by-semantics to differentiate between the two.

JavaScript is more similar to Java in that both languages have no concept of value types. All objects are references, which means all values are references too, therefore there is no need to make a distinction about call semantics. (Actually now Java has Record types which are kinda like value types, except that they are only shallowly immutable). Therefore, every time you pass an object to a function, if that object is mutable, any changes made to it will reflect outside the function.

i.e. if the Boolean type had a mutable field, you will see that mutation after passing that boolean to a function which changes it. The same will not be true in languages with true value types like C++ or C. A value type in C++, even with mutable fields will not reflect in the original object outside the function, if the function were to mutate it.

Collapse
 
hexnickk profile image
Nick K

Hey πŸ‘‹ Not sure if I'm following if this comment describing the same issue as this one or is it another issue? Can you take a look at the previous one?