0

I want to know how variables are stored in JavaScript (what I mean is, how JavaScript stores the type and the value of the variable and stuff like that).

But I was only able to find a tutorial about how the first version of JavaScript stores variables. Do newer versions of JavaScript stores variables in the same way as that first version?

4
  • 6
    There are many implementations of JavaScript (formally, ECMAscript), so talking about "old" and "new" isn't very well defined
    – Alexander
    Sep 19 at 21:25
  • The article that you referenced is talking about the original implementation of JavaScript in Netscape, long before it became a standard. These days, there are many implementations and there is no requirement that they internally store values in any specific format.
    – casablanca
    Sep 19 at 22:07
  • 3
    The ECMAScript language specification does not prescribe any particular way of representing values in memory. In fact, it does not prescribe anything about memory or representation at all. Every implementor is free to represent values in memory however they want. And in fact, almost all implementations represent at least some values differently than other implementations. V8, for example, has changed representations several times over the years. Heck, V8 even changes the representation of values at runtime sometimes. Sep 20 at 6:19
  • And then there's implementations like Narcissus (written in ECMAScript and running on the ECMAScript platform), GraalJS, dyn.js, Nashorn, Rhino (written in Java and running on the Java platform), IronJS (written in F# and running on the .NET platform), etc. which don't have a runtime memory representation at all because their platforms do not allow access to memory. Sep 20 at 6:23
1

JavaScript is a dynamically typed language. This means: variables don't have types – values/objects have types. A variable is just a label for some object. Each value/object has metadata that indicates its type.

There are a number of different JavaScript implementations that take different approaches at implementing this concept. The blob post you link mentions type tags, a popular method to discriminate primitive types. At least the V8 JavaScript engine still uses pointer tagging as part of its main object representation (see the v8-internal.h header), but things have become much more complex. For example, V8 also uses pointer tags to encode garbage collection information. And in contradiction to what I said in the first paragraph, V8 also tags fields within objects with a type.

At least within a function, you can assume that the JS engine will be able to infer a type for each variable, either through analysis of the surrounding code or speculatively. Such information can be used to JIT-compile efficient code for the function, allowing data to be processed without having to consider type tags for each operation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.