Value vs reference :
- When assigning a value to a variable then JavaScript does two things. Depending on the value, if the value is primitive, then JavaScript takes the actual value and points to a variable.
- If you declare and initialise an object or array, then the values are not directly pointing to that variable; it will be created somewhere in the memory, and a reference is assigned to that variable.
- Primitives are assigned by value, and those are number, string, boolean, null, and undefined.
- Non primitives are assigned by reference, and those are objects, arrays, and functions
var a =[10,6,87]
var a = 6hhghey : backend like this
- A variable point of the object address
Triple equal to: It will compare only primitive data types, not for non primitive Data types like objects, arrays.
Types of copies: There are two types, and they are
Shallow copy:
- Reference is allocated to another variable, both variables are pointing same memory reference, if any changes occur in the new variable, then the original variable is also affected.
- A shallow copy means that certain (sub) values are still connected to the original variable.
Example for arrays :