Search code examples
javascriptc++v8

Where and how is a value's data type stored, in JavaScript?


In JavaScript, we have 6 primitive data types (each with their own object wrapper) and 1 object data type.

Where / how does v8 store a value's data type?


Solution

  • The data type is part of the value. The type of JS values is a sum type that lets us distinguish the primitive types and objects. For example typeof is an operator that lets us access (parts of) the bit that stores the type.

    Of course, an optimising compiler is free to drop that information when it can prove that a certain variable will only ever store values of the same type, so in the implementation the information might be moved to an annotation on the variable.