If you would like to support me and make it possible for me to continue sharing resources for free, you could donate here through PayPal ♥

By value vs. by reference

By value vs. by reference

Almost everything in JavaScript is an object, such as arrays and functions. An object is a collection of name/value pairs, which values can be anything, meaning that it can be either a primitive type or another object. Objects have properties and methods (functions that sit on an object) and are sitting in the memory with references to the spots where these properties/methods “live”. You have access to those properties and methods in memory.


All primitive types interact by value.

carbon3 (32).png
Screenshot 2018-12-21 at 22.27.07.png
 

First, we declare two variables a and b. Then, we set b equal to a, which is 4 at that point. Later, we give a the value of 2. At this point, b is still 4! This happens because b has its own space in memory!


All non-primitive values interact by reference when setting them equal to each other or passing them to a function.

In memory, this would look something like:

Screen Shot 2018-12-26 at 09.37.23.png

Both c and d have the same value, which points to the same address.

Variable c holds a reference to an array (object) . Later, we assign d with the same reference that c has to the object. If you later change the value of that variable by, for example, pushing an element to that array, it also means that that property is changed for all the other variables. They point to the actual same object, and not a copy of it. Workday software training can include some basic javascript topics such as working with web forms and creating responsive web applications.

Function Constructors & Prototypes

Function Constructors & Prototypes

Classes

Classes

0