You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/value-reference-types.mdx
+26-7Lines changed: 26 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,20 +42,24 @@ JavaScript has two categories of data types that behave very differently:
42
42
43
43
| Type | Example | Stored As |
44
44
|------|---------|-----------|
45
-
|`string`|`"hello"`| The actual characters|
46
-
|`number`|`42`| The actual number|
47
-
|`bigint`|`9007199254740993n`| The actual large integer |
48
-
|`boolean`|`true`| The actual boolean |
45
+
|`string`|`"hello"`| The string value|
46
+
|`number`|`42`| The numeric value|
47
+
|`bigint`|`9007199254740993n`| The large integer value|
48
+
|`boolean`|`true`| The boolean value|
49
49
|`undefined`|`undefined`| The undefined value |
50
50
|`null`|`null`| The null value |
51
51
|[`symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)|`Symbol("id")`| The unique symbol |
52
52
53
53
**Key characteristics:**
54
-
-Stored directly in the variable
54
+
-Behave as if stored directly in the variable
55
55
- Immutable — you can't change them, only replace them
56
56
- Copied by value — copies are independent
57
57
- Compared by value — same value = equal
58
58
59
+
<Info>
60
+
**Under the hood:** Modern JavaScript engines optimize string storage through a technique called "string interning" — identical strings may share the same memory location internally. However, this is an optimization detail; strings still *behave* as independent values when you work with them.
61
+
</Info>
62
+
59
63
### Reference Types
60
64
61
65
**Everything else** is a reference type:
@@ -119,6 +123,10 @@ This difference between "storing the value itself" vs "storing a map to the valu
119
123
120
124
To truly understand the difference, you need to know where JavaScript stores data.
121
125
126
+
<Info>
127
+
**Important note:** The stack/heap model described below is a **conceptual simplification** to help you understand how value types and reference types *behave*. The JavaScript specification doesn't define where values are stored in memory—actual engines (V8, SpiderMonkey, etc.) may optimize storage differently. What matters is understanding the *behavior* difference, not the physical memory location.
128
+
</Info>
129
+
122
130
### The Stack: Home of Primitives
123
131
124
132
The **stack** is a fast, organized region of memory. It stores:
**Caution with JSON.stringify:** Property order matters! `{a:1, b:2}` and `{b:2, a:1}` will produce different strings even though they're logically equal. It also fails with `undefined`, functions, Symbols, circular references, `NaN`, and `Infinity`. For reliable deep equality, use a library like Lodash's `_.isEqual()`.
To freeze nested objects too, you need a recursive "deep freeze" function:
609
619
610
620
```javascript
611
-
functiondeepFreeze(obj) {
621
+
functiondeepFreeze(obj, seen=newWeakSet()) {
622
+
// Prevent infinite loops from circular references
623
+
if (seen.has(obj)) return obj;
624
+
seen.add(obj);
625
+
612
626
// Get all property names (including symbols)
613
627
constpropNames=Reflect.ownKeys(obj);
614
628
615
629
// Freeze nested objects first
616
630
for (constnameof propNames) {
617
631
constvalue= obj[name];
618
632
if (value &&typeof value ==="object") {
619
-
deepFreeze(value);
633
+
deepFreeze(value, seen);
620
634
}
621
635
}
622
636
@@ -633,6 +647,10 @@ user.address.city = "LA"; // Now this is blocked too!
633
647
console.log(user.address.city); // "NYC"
634
648
```
635
649
650
+
<Info>
651
+
**Why the `seen` WeakSet?** Objects can have circular references (e.g., `obj.self = obj`). Without tracking visited objects, the function would recurse infinitely. The WeakSet ensures each object is only processed once.
652
+
</Info>
653
+
636
654
### Related Methods: freeze vs seal vs preventExtensions
0 commit comments