Writing something like 5 + 5
is a valid expression and doesn't throw any error even if it isn't assigned to a variable. Is there anywhere this expression is maintained in memory or does it just disappear once the addition is completed?
Asked
Active
Viewed 45 times
0

1252748
- 14,597
- 32
- 109
- 229
-
If it is not assigned anywhere it is up to the implementation whether or not to retain it. Typically in consoles it is retained for debugging (try typing 5+5 in the chrome console, press enter and then type `$_`) - Most implementations would not keep a reference to it and the memory would get garbage collected. The JavaScript specification itself does not dictate any specific behavior here. – Benjamin Gruenbaum Aug 30 '14 at 18:09
-
1It is also worth mentioning that clever compilers will see that expression in a file and optimize it away so code without side effects like `5 + 5;` in a JavaScript file may actually never run to begin with. – Benjamin Gruenbaum Aug 30 '14 at 18:11
-
@BenjaminGruenbaum I'm aware of `$$` in the console in Chrome, but what is the `$_` syntax? – 1252748 Aug 30 '14 at 18:16
-
It means "the last evaluated expression" - which is the 5 + 5 in your case. – Benjamin Gruenbaum Aug 30 '14 at 18:17
1 Answers
0
There are no references to the result, so it is eligible for garbage collection. The memory will be freed at some point in the future, when the JavaScript runtime decides to schedule a GC run.
See comments - it probably won't be stored on the heap at all.
See the MDN docs for more on JavaScript memory management.

joews
- 29,767
- 10
- 79
- 91
-
2I would not expect primitives (except strings maybe) to enter the heap at all, so they don't need to be garbage collected. – Bergi Aug 30 '14 at 18:14
-
@Bergi Why would you not expect this? And why would the expectation be different for strings? Thanks. – 1252748 Aug 30 '14 at 18:17
-
1@thomas depends on the engine, strings are interned (read: cached) by some JS engines so some strings might not ever get GC'd at all. What Bergi said is that they'll get allocated on the stack and not on the heap to begin with. – Benjamin Gruenbaum Aug 30 '14 at 18:19
-
@Bergi but if typing `5 + 5` in the console prints `10` when I next type `$_` doesn't that indicate it is "entering the heap"? – 1252748 Aug 30 '14 at 18:19
-
@BenjaminGruenbaum Okay. Clearly I am out of my depth regarding stacks and heaps. I'm not that aware of the internals. Are you aware of any good introductory articles? – 1252748 Aug 30 '14 at 18:20
-
@thomas in the case of `$_` (which by the way only works in the console and not in .js files) it _does_ enter the heap, but generally speaking V8 (assuming Chrome) has very effecient handling of SMIs (small integers) - you can read about it in the v8 introduction videos, and http://mrale.ph/ – Benjamin Gruenbaum Aug 30 '14 at 18:23
-
According to http://stackoverflow.com/a/3691209/2806996, primitives are stored on the heap, too. I don't know details of specific implementations, though. – joews Aug 30 '14 at 18:24
-
@joews, thomas: It all depends on the optimisations done by the compiler, of course any values could be put on the heap but that doesn't make sense. In `var x = 5 + 5` I'm quite sure that the fives are put on the stack (if space is "allocated" for them at all), and the resulting ten goes wherever `x` is allocated - which may be in the heap (especially when the variable is referenced from a closure), but could as well be on the stack so it is cleared when the function finishes, not when the GC runs. And if `5 + 5` is not assigned to a variable, the ten just vanishes immediately. – Bergi Aug 30 '14 at 18:31