Skip to content

TodayILearned- 2023-03-20

Posted on:March 20, 2023 at 06:42 AM

TIL

Today’s TIL is on objects. Tl;dr, I love the ... syntax. 💖

difference between null and undefined

Primitive values vs. reference values:

Primitive values:

Reference values:

For primitive values, copying/cloning the VALUE that a variable holds to another is easy. you simply assign the second variable to the first. for instance:

const numberOne = 3;

// you want to copy that value of 3 to this variable
const secondNumber = numberOne;

The thing about primitive values is that, if you now change the value of secondNumber = 5 and console.log both numberOne and secondNumber, they are now two different variables holding two different values. This is because essentially, numberOne and secondNumber are two separate “boxes,” per se. These two boxes initially held the same value of 3, but one has changed.

console.log(numberOne); // 3
console.log(secondNumber); // 5 

However, this is NOT the case for reference values - if you use the same const object2 = object1 syntax, what you’re copying isn’t the data stored inside itself, but the REFERENCE - aka the ‘address in memory.’ In other words, when you make a copy of object1, you aren’t actually creating another brand new, independent object. You’re simply copying its address - so now, having object1 and object2 is simply equivalent to having two separate road signs that still point to one existing address.

Therefore, if you have the following code:

const object1 = {
   name: 'Boyeon',
   location: 'Korea',
}

// create a 'copy' of object1
const object2 = object1; 

and you try to change the object2.location:

object2.location = 'France';

console.log(object1, object2); // {name: 'Boyeon', location: 'France'}   {name: 'Boyeon', location: 'France'} 

You notice that the change in the location key-value pair is reflected across both object1 and object2 - this is because both objects are pointing to the same address, the same point in memory. It is for this reason that objects and arrays are called reference values.

Conversely, this is why if you create two objects separately, like the following:

const oneObject = {};
const secondObject = {};

And you try to see if they are equal by console logging oneObject === secondObject, you will get false, even though the contents of the two objects are empty (and thus the same). Why? Because these two objects were created separately, they each are pointing to two different places in memory - kind of like having two separate addresses to two empty houses. Are both houses(objects) empty? Yes. But are they located in the same address(memory)? No.

So how can we create an independent copy of an object from an already existing one? Enter the ... spread syntax.

const onePerson = {name: 'Max', age: 29}
const anotherPerson = {...onePerson}; 

anotherPerson.name = 'Minnie'

console.log(onePerson, anotherPerson) // {name: 'Max', age: 29}, {name: 'Minnie', age: 29}

A little CAVEAT though - the spread operator does not create a deep clone - that is, if the object has nested objects or arrays inside it, the reference values will be copied.

I’m pretty proud of the progress that I’ve been making on the udemy course — and it has definitely filled in some of the gaps that I’ve vaguely known about my JavaScript knowledge. Onwards, to another gap in my knowledge.