I am confused about how memory allocation and function calls tracking happen in Javascript. One thing I am quite sure about is that there is a call stack in JS, where function calls are stored in LIFO manner. But when it comes to memory allocation, I am confused which of the following arguments is correct:
- Primitive variables like string and numbers, are stored in something called as stack, which is different from call stack. Whereas non-primitive variables like objects and functions are stored in heap memory, but their references are stored in stack. (As per these articles: https://blog.alexdevero.com/memory-life-cycle-heap-stack-javascript/, https://felixgerschau.com/javascript-memory-management/ and https://felixgerschau.com/javascript-event-loop-call-stack/)
or
- There are only two things: Call stack and heap memory. Call stack will store function calls in LIFO manner. Whereas heap memory will store variables whether primitive or non-primitive. (As per these videos: https://www.youtube.com/watch?v=7rOpIX-7ErA&t=32s, https://www.youtube.com/watch?v=xFNWb7KiG58)
If 1st argument is correct, then how call stack and stack are connected to each other such that function in call stack can identify which variables to pick from stack for its execution?