JavaScript's Memory Management Explained
Having a look at the heap, stack, and how garbage collection works.
Hey 👋
I’ve published a new article, explaining one more aspect of how JavaScript works in the browser.
This one focuses on how memory management works.
Even though the JS engine handles this for you, it’s good to know what happens under the hood to prevent memory leaks and other bugs from happening.
JavaScript's Memory Management Explained
In the article, I cover what the JS heap and stack are responsible for. Furthermore, I’ll have a look at how garbage collection works and what you can do to prevent memory leaks.
I hope you’ll find this article useful; I certainly learned a few new things while writing it.
Have a nice week and see you soon!
Hey Ankur, yes I can elaborate on that:
Immutable means that the value itself doesn't change.
Take this example:
var x = 'one'
x = x.substring(0, 1)
On line 1, JavaScript stores the string 'one' in the stack. On the second line, instead of modifying the original value in memory, it creates a new string based on the original value.
The variable x now points to the new string 'o' instead of 'one', but the original value (the string 'one') is never modified in memory.
Here's an article that clarifies this further: https://bit.ly/3HVgSkA
Does this clear things up for you?
Hi, could you please explain:- Primitive values are immutable, which means that instead of changing the original value, JavaScript creates a new one.
How does this happen? Could you please explain the detailed working of this?
Thanks.