1

I'm beginner in web frontend development. I have some questions, and one of them is whether hoisting in JavaScript has to do with spatial locality for memory optimization. The reason I thought so was as follows.

JavaScript seems to follow call-by-reference except for primitive types. And, in the process of allocating memory for variables in JavaScript, keywords(var, let, const) are found and memory is allocated for references first.

So, I thought of this as putting the addresses of references on the same page as much as possible. I'm curious if anyone knows about this or what other people think. Thank you for reading my question :)

Heojoooon
  • 11
  • 1
  • Have a [read of this](https://amandafalke.com/tutorials/2017/04/12/intro-to-modern-javascript-for-cpp-developers.html) – mplungjan Feb 01 '23 at 11:27
  • Not at all related. The reason "hoisting" exists *as a concept* is to explain two-phase interpreter pass. The first time around it reads code and checks for conformity and prepares execution. In relation to that, it sets aside which identifiers are or aren't used in order for the execution to be able to tell if `console.log(a)` is logging a variable `a` or just a mistake since `a` doesn't exist. The variables themselves don't "set aside memory" as you can't really do that for dynamic data (note: some environments may do such an optimisation but it's an internal detail) – VLAZ Feb 01 '23 at 11:28
  • Relevant: [Why does JavaScript hoist variables?](https://stackoverflow.com/q/15005098) and [How does hoisting work if JavaScript is an interpreted language?](https://stackoverflow.com/q/45620041) – VLAZ Feb 01 '23 at 11:31

0 Answers0