In JavaScript, a Map stores keys and values by reference when they are objects.
This means Maps do not create copies of objects; instead, they store a reference to the original object in memory.
Understanding Map reference behavior is important when working with real-world data structures.
What Does Reference Mean in a Map?
-
Primitive values (string, number, boolean) are stored by value
-
Objects, arrays, and functions are stored by reference
-
Changing an object affects all places where its reference is used
Reference Behavior with Object Keys and Values
Output:
{ status: "inactive" }
Explanation:
-
The Map stores references, not copies
-
Updating
valueObjupdates the data inside the Map -
Both point to the same object in memory
Same Content, Different References
Explanation:
-
Even though
obj1andobj2look identical -
They are different references
-
Map treats them as separate keys
Reference with Arrays in Map
Output:
[1, 2, 3, 4]
Explanation:
-
Arrays are objects
-
Map stores the reference
-
Any change to the array is reflected in the Map
Why Map Reference Matters
Understanding Map reference behavior helps you:
-
Avoid unexpected data mutations
-
Manage shared objects safely
-
Debug>
Build reliable applications
-
Work confidently with complex data
Quick Summary
-
Maps store objects by reference
-
Changes to objects affect Map entries
-
Identical-looking objects are different if references differ
-
Primitives behave differently than objects
-
Important for real-world data handling
Conclusion
Map reference in JavaScript explains how Maps handle objects internally.
Once you understand reference behavior, you can manage shared data correctly and write predictable, bug-free JavaScript code.