Object Assignment vs. Primitive Assignment in JavaScript for Beginners
Nick Scialli
July 27, 2019
Introduction
Something I wish I had understood early on in my JavaScript programming career is how object assignment works and how it’s different from primitive assignment. This is my attempt to convey the distinction in the most concise way possible!
Primitives vs. Objects
As a review, let’s recall the different primitive types and objects in JavaScript.
Primitive types: Boolean, Null, Undefined, Number, BigInt (you probably won’t see this much), String, Symbol (you probably won’t see this much)
Object types: Object, Array, Date, Many others
How Primitive and Object Assignment Differ
Primitive Assignment
When you assign a primitive value to a variable, the value is created in memory and the the variable points to that value in memory. Primitives can’t be modified; in other words they are immutable.
Let’s look at the following code:
const a = 'hello';
let b = a;
In this case, the string hello
is created in memory and a points to that string. When we assign b
the value of a
, b
also points to the same hello
string in memory. Now what if we assign a new value to b
?
b = 'foobar';
console.log(a); // "hello"
console.log(b); // "foobar"
This makes sense; a new string (foobar
) is created in memory and now b
points to that string. a
still points to hello
in memory. Importantly, nothing we do to b
will affect what a
points to because primitive values are immutable.
Object Assignment
Object assignment works similarly in that the object is created in memory and the variable points to that object. However, there is one key difference: objects can be mutated! Let’s take a look at a new example.
const a = { name: 'Joe' };
const b = a;
The first line creates the object { name: 'Joe' }
in memory and then assigns a reference to that object to variable a
. The second line assigns a reference to that same object in memory to b
.
Again, this is a big deal for objects because they’re mutable. Let’s now change the name
property of the objec that b
is pointing to:
b.name = 'Jane';
console.log(b); // { name: "Jane" }
console.log(a); // { name: "Jane" }
That’s right! Since a
and b
are assigned a reference to the same object in memory, mutating a property on b
is really just mutating a property on the object in memory that both a
and b
are pointing to.
To be thorough, we can see this in action with arrays as well.
const a = ['foo'];
const b = a;
b[0] = 'bar';
console.log(b); // ["bar"]
console.log(a); // ["bar"]
This Applies to Function Arguments too!
These assignment rules apply when you pass objects to functions too! Check out the following example:
const a = { name: 'Joe' };
function doSomething(val) {
val.name = 'Bip';
}
doSomething(a);
console.log(a); // { name: "Bip" }
The moral of the story: beware of mutating objects you pass to functions unless this is intended (I don’t think there are many instances you’d really want to do this).
Preventing Unintended Mutation
In a lot of cases, this behavior can be desired. Pointing to the same object in memory helps us pass references around and do clever things. However, this is not always the desired behavior, and when you start mutating objects unintentionally you can end up with some very confusing bugs.
There are a few ways to make sure your objects are unique. I’ll go over some of them here, but rest assured this list will not be comprehensive.
The Spread Operator (…)
The spread operator is a great way to make a shallow copy of an object or array. Let’s use it to copy an object.
const a = { name: 'Joe' };
const b = { ...a };
b.name = 'Jane';
console.log(b); // { name: "Jane" }
console.log(a); // { name: "Joe" }
A note on “shallow” copying
It’s important to understand shallow copying versus deep copying. Shallow copying works well for object that are only one level deep, but nested object become problematic. Let’s use the following example:
const a = {
name: 'Joe',
dog: {
name: 'Daffodil',
},
};
const b = { ...a };
b.name = 'Pete';
b.dog.name = 'Frenchie';
console.log(a);
// {
// name: 'Joe',
// dog: {
// name: 'Frenchie',
// },
// }
We successfully copied a
one level deep, but the properties at the second level are still referencing the same objects in memory! For this reason, people have invented ways to do “deep” copying, such as using a library like deep-copy
or serializing and de-serializing an object.
Using Object.assign
Object.assign
can be used to create a new object based on another object. The syntax goes like this:
const a = { name: 'Joe' };
const b = Object.create({}, a);
Beware; this is still a shallow copy!
Serialize and De-serialize
One method that can be used to deep copy an object is to serialize and de-serialize the object. One common way to do this is using JSON.stringify
and JSON.parse
.
const a = {
name: 'Joe',
dog: {
name: 'Daffodil',
},
};
const b = JSON.parse(JSON.serialize(a));
b.name = 'Eva';
b.dog.name = 'Jojo';
console.log(a);
// {
// name: 'Joe',
// dog: {
// name: 'Daffodil',
// },
// }
console.log(b);
// {
// name: 'Eva',
// dog: {
// name: 'Jojo',
// },
// }
This does have its downsides though. Serializing an de-serializing doesn’t preserve complex objects like functions.
A Deep Copy Library
It’s fairly common to bring in a deep copy library to do the heavy lifting on this task, especially if your object has an unknown or particularly deep hierarchy. These libraries are typically functions that perform one of the aforementioned shallow copy methods recursively down the object tree.
Conclusion
While this can seem like a complex topic, you’ll be just fine if you maintain awareness about how primitive types and objects are assigned differently. Play around with some of these examples and, if you’re up for it, attempt writing your own deep copy function!
Nick Scialli is a senior UI engineer at Microsoft.