Advice On Combatting Disordered Eating During Quarantine

Everything tasted like soap. Plump, red tomatoes could only be distinguished from grainy pasta or the capers they were mixed with because each had a distinct shape, size, and texture. From family…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Primitive types vs Reference types in Javascript

Hi! I want to share with you the difference between Primitive types and Reference types in Javascript.

What is a Primitive type?

In Javascript there are six primitive types:

Primitive means creating a copy of a value. What does that mean?

Ex.

Notice here we created a variable named “movie” and a variable named “secondMovie” that has a COPY of the value of movie. It’s very important to understand that we are not referencing the same value as we are dealing with a primitive type. Now once we change the value of “movie” notice how “secondMovie” still retains it’s original value. This is a result of creating a COPY of a value.

What is a Reference type?

Reference type is when an identifier points to value in memory. (Typically Objects, Arrays and Functions)

Ex.

Notice here we variable named “character” which is an object with a key of “name” and value of “The Hulk”. We create a POINTER to character when we create “alsoCharacter”. So once we change the value of key “name” to “Iron Man” and console log “alsoCharacter” it will reflect the change as it pointed to value that was changed.

Main differences between primitive types and reference types.

One of the main differences between primitive types and reference types is how they are stored in memory.

Primitive types are stored in the stack.

Notice here that once we create the variable “secondMovie” we create a COPY of “movie” and add it to the stack. Where as when dealing with reference types we create a new pointer rather than recreating the entire object.

Another way to spot these differences is “===” when comparing references.

Ex 1

Ex 2

Notice here that when using “===” on reference types if the references match then it will return true. In the second example we have created two individual arrays in memory with the exact same values, however as the references don’t match we return false.

Additional Resources:

Thanks for reading! Any feedback or questions please feel free to reach out! Have a great day!

Add a comment

Related posts:

Corona Times

Je zal me nooit een (papieren) boek zien lezen zonder potlood en post-it’s bij de hand. Vroeger vond ik het heiligschennis. Tegenwoordig onderstreep, plak en plooi ik hoeken om dat het een lieve…

Top audiobooks for Startup Founders

Inspired by the recent HN thread, I’ve decided to share a collection of my favorite audiobooks and podcasts that I think HN readers will love. This is the best stuff I have discovered over the past…

About Frontify

Having a strong brand presents multiple advantages for a business. When creating and maintaining a strong brand; nothing — not one thing — is more important than brand consistency. Contemporary…