JavaScript has 5 data types that are copied by value. We call these primitives:
– Boolean
– null
– undefined
– String
– Number

And 3 data types that are copied by reference. We call these Objects:
– Array
– Function
– Object

Pure functions

Any function that doesn’t affect anything outside it’s outside scope is called a pure function. A function made up of primitives is a pure function. An Array passed in as an argument, that gets something pushed onto it, would affect the outside scope array. It’s change would persist to the outer scope and can lead to bugs that are hard to track down.

That’s why and Array.filter return copies of the Array they operate on and were written as pure functions.


See if you can decode what’s going on here.

Screen Shot 2017-08-29 at 1.45.07 PM.png

We start off with personObj1, pass it into the function, assign it an age of 30, and then point it to a new object instance before returning and assigning it to a new variable.

Here is the actual output

Screen Shot 2017-08-29 at 1.46.42 PM.png

Get it? personObj1 is passed by reference to the method. So method has a pointer to personObj1 and assigns it a new age of 25. It’s done.

Screen Shot 2017-08-29 at 1.47.56 PM.png

This next line of code is the tricky one.

Screen Shot 2017-08-29 at 1.48.30 PM.png

This chunk of code reassigns the pointer that was pointing to personObj1 to a new object – John 50 and returns it.

Screen Shot 2017-08-29 at 1.49.25 PM.png

The reassignment does not change personObj1. It remains intact. person has a new reference because it was reassigned. But reassign doesn’t change original. The equivalent above would be the following.

Screen Shot 2017-08-29 at 1.51.17 PM.png

Links that help

These notes were based on the excellent links found here: