34

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.

3
  • Several answers were posted, but I'd say the real question is: why do you need this? Commented May 25, 2018 at 14:07
  • and what about varables in Typescript ? Commented May 25, 2018 at 14:17
  • 1
    I need this answer because I read so many posts and documents for pass by value and pass by reference but not even a single person is sure about the reality due to memory management. Commented May 25, 2018 at 14:20

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For pass by reference we need to know memory address of the identifiers.
7

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.

Comments

6

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.