ObjectAsParameterIsCopied
DRAFT

Misconception:

When an object is passed as an argument, its whole content gets copied into the called function’s formal parameter.

Incorrect

Objects are passed by value

Correct

Objects are passed by reference

Correction
Here is what's right.

When we deal with objects, we actually manage references to objects. A statement like

let obj = {name: "progmiscon"};

creates an object in memory that contains the property name with a certain value and stores the reference to that object inside the variable obj. Among other things, this implies that when obj is passed as an argument to a function, it is the reference that is being passed, not the whole object that resides in memory.

Consider this code:

The reference to the object with name "progmiscon" is copied into the parameter o of the function change. When we try to access the name property, we are referencing the only object that exists in memory (and that was not copied) and changing the value of the property.

As a result, when we later on inspect the object from the “main” code, we see the modification that occured to the object in memory.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.