Why explain a problem. Just give me the solution?

Because understanding what is difficult or does not work, can give us a deeper understanding of why and often lead us to look for, or develop solutions, that will work. A link to a great video is at the end of this story.

Primitive data types versus Reference (non-primitive) types

Essentially it has to do with JavaScript primitives versus JavaScript non-primitives (objects).

As I will demonstrate, through code and analogy, variables containing a primitive type are given a value in memory. Variables containing a non-primitive type are given a reference in memory.

Primitive type and memory allocation

Example 1

The primitive types, keeping it simple, are number, string, boolean, undefined and null. A full list can be found here.

Primitives have a value in memory

What’s happening? For any primitive, memory is allocated and the value of the primitive is stored in that memory location.