JavaScript is a Garbage Collected Language.
Within a function, we create an object and an object gets stored in our memory heap automatically. If we finish calling the function, it will be clean up automatically. only the data that is still useful to us remains. However, there is no perfect system, so it gives JavaScript developers the false impression that they do not care about memory management
.
function subtractTwo(num){
var human = "me";
return num -2;
}
subtractTwo(3)
When the function is done and it is popped off from the stack then we don’t need human
variable anymore. So, the Garbage collector is going to mark
and sweep
(clean up) the variable from our memory heap.