Hacker News new | ask | show | jobs
by potch 3063 days ago
JavaScript is pass-by-reference for all Objects and Arrays. Value types are pass-by-value.

    let obj = {a: 1}
    
    function foo(arg) {
      return obj === arg;
    }
    
    foo(obj) // true
The === operator operates on object not by comparing their value, but by comparing their memory reference [1]. A function argument variable can be reassigned using = in the function body, but that changes which location in memory the reference points to and isn't somehow "proof" of pass-by-value.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

1 comments

Javascript is pass-reference-by-value for Objects and Arrays, see: https://jsfiddle.net/t73ykuj0/

If javascript was pass-by-reference for Objects, your code could read: arg = {a: 2}; return obj === arg;

and still behave the same.