Skip to content
Advertisement

If I change a variable’s value will the original value still exist inside the memory?

JavaScript

In this case, the value 7 is still inside a memory? I was reading that all primitive data types are immutable.

Advertisement

Answer

The value of a is kept in memory, until garbage collection eventually recycles it. What the docs mean by immutable is that you can’t directly alter the primative (in this case the integer 7). You can only replace the value.

There are examples on the docs but this is another one

JavaScript

However, we can assign this to another variable

JavaScript

Or we can replace the value

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement