JSVerse
Advanced JavaScript / Object References

Object References

Variables holding objects hold an address, not the object itself — two names can point to one thing.

Primitives are copied by value: assigning `b = a` copies a's actual value into b, and the two become independent. Objects (and arrays, and functions) work differently — a variable holding an object actually holds a reference, a pointer to a shared location in memory. Assigning `b = a` for an object copies the pointer, not the object, so `a` and `b` end up pointing at the exact same thing. Mutating through either name affects what both see.

Codeeditable — Run executes your edits
Loading...
Console
Output will appear here
Visualization
References & the Heap
Create an object to see it on the heap
Why?

b = a never created a second object — it copied the reference. a and b are two different names pointing at one shared object in the heap, so mutating through b is visible through a too.