1

So, we know that value of variables that contain objects in javascript are passed by reference. for example:

let a = {name:'john'};
let b = a;
a.name = 'doe';
console.log(b.name)//prints out doe

but:

let a = {name:'john'};
let b = a;
a = {name: 'not john'}
console.log(b.name)//should printing not john, but instead keeps showing john

How this is possible ? Value of variable a are replaced with a new object with a different value. Object with a name property john are gone and variable b should refer to variable a with property value 'not john' but it keeps refering to an objects which doesn't exist !

  • 2
    Does this answer your question? [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – evolutionxbox Dec 23 '20 at 10:30
  • AFAIK JavaScript is neither pass-by-value or pass-by-reference. – evolutionxbox Dec 23 '20 at 10:30
  • 1
    @evolutionxbox - JavaScript is pass-by-value (purely). – T.J. Crowder Dec 23 '20 at 10:31
  • 1
    @T.J.Crowder isn't it ["call_by_sharing"](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing)? – evolutionxbox Dec 23 '20 at 10:32
  • 1
    *"So, we know that value of variables that contain objects in javascript are passed by reference."* You need to be careful with your terminology here. "Pass by reference" is a term of art that means, specifically, passing a **variable** by reference, for instance pushing its memory address on the stack to be retrieved by the callee so the **variable**'s value can be changed from the called context. JavaScript doesn't do that, ever. Ojbects are not passed by reference. Object references are passed by value. – T.J. Crowder Dec 23 '20 at 10:33
  • 1
    @evolutionxbox - Some people may use that term instead for passing object references around (JavaScript would still be pass-by-value for primitives). It's not commonly used, though. Most people talk in terms of object references being values, and pass-by-value. – T.J. Crowder Dec 23 '20 at 10:36
  • 1
    @T.J.Crowder what is an "object reference" then? If we are "careful with terminology", there is no such thing as a reference in JavaScript – Jonas Wilms Dec 23 '20 at 10:38
  • 2
    *"How this is possible ?"* When you do `let b = a;` you're **copying** an *object reference* from `a` to `b`. That object reference says where the object is elsewhere in memory. There is no ongoing link between `a` and `b` at that point, they just both happen to contain a reference to the same object. When you change what `a` contains (by storing a different object reference in it), that has no effect at all on what `b` contains. `b` continues to contain the first object's reference. – T.J. Crowder Dec 23 '20 at 10:39
  • 2
    @JonasWilms - It's true that the spec avoids using the term for that (not least because it uses "reference" for the other thing). Let's not get into a whole discussion about it in comments on a newbie question. :-) Happy holidays! – T.J. Crowder Dec 23 '20 at 10:43
  • 1
    @T.J.Crowder Got it. Thank you for clearing it out for me ! – Олег Войтинський Dec 23 '20 at 10:46

0 Answers0