Almost everything is an object

JavaScript has Primitive Data types and Reference Data types. number , string , boolean , symbol , null , and undefined are primitive data types. Primitive data type means when you create a variable, store a value of any given primitive data type in it, then declare another variable which is assigned (with the value) to the first variable, the second variable receives only a copy of the value from the first variable. Let’s see that in practice.

In the above example, we can see that variable name_max was assigned with variable name_john . Since the string is a primitive data type, name_max received only copy of the value from name_john . Later, we assigned a new value to name_max . This should change only the value stored in name_max and should not affect name_john in any way, which is proved here.

Reference data types work in different fashion. array , object and function are reference data types. When you create a variable A of reference data type (by assigning with a value of reference data type), the value is stored in the Heap and its memory reference is stored in the variable (this happens internally and A is not a pointer). When you assign another variable B with the variable A such as B=A , instead of copying the value of A in the variable B , the variable B only gets the memory reference of the value A points.

But unlike other programming languages, if you assign a new value to the variable B , it won’t change the value of A . JavaScript will instead assign a new memory in the Heap and store the value there. Here, A and B are now referencing different values in the memory.

But instead of assigning a new value to B , if we are assigning a new value to the property of B , JavaScript will use the referenced object and modify the property-value on it. Since A is still pointing the same value in the memory, A will get affected here. Hence, only in process of mutation of an object, JavaScript references the value. Let’s see that in action.

In the above example, we created variables morning , mike & odd and then assigned a value of function , object and array . Later we created variables evening , john & even and assigned the given variables to them. Since, morning , mike & odd points to the value of referenced data types, evening , john & even will get only references to these values.

From the result, we can see that any changes made to the properties of the evening , john and even modifies the value they point. Hence morning , mike & odd are also modified. But in the case of kate , since we are replacing the entire value, JavaScript allocates new memory for the value and mike remains unaffected, as expected.

One weird thing you might have noticed in the above example is that we are adding a property info on the morning variable which is a function . We know that we can add properties on an object, because traditionally an object is a map of keys and values like {x:y,} . Then how this is possible?

In JavaScript, all referenced data types are objects, not in the traditional way though. Any variable which has constructor property on it, is an object. I will explain constructor in details later, but the important thing here to notice is that constructor is like a function from which the object was created. If x.constructor syntax returns a function, then x is an object.

Not only that, all primitive data types except null and undefined are objects. A string is generated from String constructor, a number is generated from Number constructor, a boolean is generated from Boolean constructor and symbol is generated from Symbol constructor. Whenever we use literal syntax, JavaScript uses constructor functions behind the scene.