Tomer Shmaya
Jul 10, 2017 · 1 min read

A great explanation of the concept, yet you stopped just before the last step — the step where all confusion arises (and is frequently used in interviews). Even though your article explains it implicitly, you did go in length to explain the copies made to function’s local primitive variables, so it would be nice to finish it with objects variables.
When an object is passed to a function the local variable gets the address “value”, and if we replace that object within the function it gets a new object/address in the memory, that will NOT affect the original, and will expire (no references left) at the end of the function’s scope. So:

var obj = {val: 1};
changeObj(obj);

console.log(obj.val); // 1

function changeObj(o) {
o = {val: 2};
console.log(o.val); // 2
}

    Tomer Shmaya

    Written by