As I know there are 4 primitives in JS (those that store value directly, rather than reference to another memory location) - String, Number, Boolean, Symbol
. I am not counting undefined, null
- as they are special data-types, and don't share Object-Constructor via inheritance-chain.)
Now, another property of primitives
is that they are Immutable
or unchanging
. Their values cannot be changed. I conclude that means, for the same variable - the values should not change (ever).
Now, kindly explain to me, in the following cases - how these data-types remain unchanged?
var str = "Good Morning";
var bool = true;
var num = 29;
str = str.replace("M", "Z");
bool = !bool;
num+=5;
console.log(str, bool, num);
For same variable, I have changed all - string, bool, number
. Then how these variables (of respective primitive data-types) are Immutable in JS?