28

Is it possible to find the memory address of a JavaScript variable in modern Javascript? Or address of Typescript variable.

I am confused about the pass by value and pass by reference, using memory address it is easy to understand the reality. Now we have now ES8 and Typescript but still unable to get memory address of variable and Object.

SAURABH
  • 283
  • 1
  • 3
  • 11

3 Answers3

12

From this post:

It's more or less impossible - Javascript's evaluation strategy is to always use call by value, but in the case of Objects (including arrays) the value passed is a reference to the Object, which is not copied or cloned. If you reassign the Object itself in the function, the original won't be changed, but if you reassign one of the Object's properties, that will affect the original Object.

R.F. Nelson
  • 2,254
  • 2
  • 12
  • 24
6

All JavaScript runtimes that I know of hide this from you.

However, you can simulate the behavior of a memory pointer by using Typed Arrays and an appropriate buffer view based on the type of data you wish to store. You can simulate a pointer by saving your data at a particular offset, and then passing that offset around to different functions that manipulate the data directly in the typed array: aka the offset acts sort of like a low-level pointer.

ouni
  • 3,233
  • 3
  • 15
  • 21
5

JavaScript itself is meant to be implementation-agnostic, so concepts like memory addresses are intentionally absent from the language itself. Outside of the language, you can use the browser's debugging tools to take a memory snapshot, and that might contain the information. Note, however, that there is no real guarantee that an object will retain its address.

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22