What is the difference between primitive and non-primitive data types? When are both used? Can you use them synonymously?
javascript - Successfully changed the Immutable or Primitive Data-Types in JS. Then How these are Primitives or is JS Concepts wrong? - Stack Overflow
Everything in JavaScript is an object...what about primitive data types?
Confusion between Objects and primitive data types in Js
Videos
I am new to javascript and still cannot understand the difference.
Thanks!
You are not mutating the existing primitives. You're simply assigning new primitive values (which are immutable as well) to the existing variable names.
Mutation is not the same thing as variable name reassignment. Mutation looks something like the following:
someObj.someProp = newPropVal;
Reassignment is:
someVarName = newVal;
Primitives don't have own-properties; trying to assign to a property of a primitive doesn't do anything and will throw an error in strict mode, because primitives are immutable.
Here's one way of looking at it (it's a nice visualization, though it's necessarily an accurate reflection of what's actually happening in the bytecode): values reference a location in memory. Variable names point to locations in memory. Say that false corresponds to memory location 1234, and you create an object {} which corresponds to memory location 9999. You can assign false to a variable name and have that variable name point to location 1234 in memory. You can reassign the value that the variable name points to by using location 9999 instead, that points to the object. The object, unlike the primitive, is a potential container for other values (and other memory locations). The primitive cannot act as such a container.
your means to change primitives are only reassigning the variable's reference to the stack
var a = 1;
a = 2;
here a is a reference address to the execute stack. Above reassignment only changes the address in stack, not 1 itself. If 1 is stored in address ****, after the reassignment, 1 is still located at ****, but a now points to 2's address in stack