DEV Community

Discussion on: Javascript academy #1: Primitive value vs reference value

vonmehdi profile image

I want to ask u someting, how to delete a object? to make his space free to use again.

codeoz profile image
CodeOz Author

I will make another topic but shortly, the memory of the Heap (where object are living and stored) is handled by garbage collector.

When you create an object you store the reference in this variable, but if you change this variable, you lost the reference into this object, so there is no access for it! And the garbage collector will delete this object.

A quick example

let a = { toto: 'hello' }
let b = a 
// Both variable store the reference of { toto: 'hello' }

a = 55
// b is the only variable that keep the reference of the object

b = 60
// There is no variable that keep this reference, so the Garbage collector will delete the object

Enter fullscreen mode Exit fullscreen mode
vonmehdi profile image

Thanks I understand it, js is really a smart language.

Some comments have been hidden by the post's author - find out more