Memory Heap: where objects are stored
How two variables access the same object in memory.
When a program creates an object { hp: 100 }, its data needs to be stored somewhere. V8 stores objects in a memory area called the heap.
Consider how an object is created and a second reference appears:
const player = { hp: 100 };
const target = player;
target.hp = 80;
// player.hp is also 80
const player = { hp: 100 }creates a player object. In the interactive diagram, V8 allocates heap memory for it and stores itshpproperty there. The variableplayerreceives a reference to this object.const target = playerreads the value ofplayerand assigns it totarget. This copies the reference: both variables now provide access to the same object. No second object is created.target.hp = 80follows the reference to the object and changes itshpproperty. The references themselves stay the same. As a result,player.hpalso becomes80.
playertarget{ hp: 80 }target.hp = 80. Both arrows lead to the same object.The diagram shows the connections between the variables and the object. The engine determines the actual memory layout. Below, we will execute the example line by line: the object, references, and property values appear as execution proceeds.