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?
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?
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.
Your only access to these types is with typeof.
There are more primitive types but they are not visible in a normal JavaScript environment. If you want to see how these are handeld inside the engine I recommand watching this video I coincidently watched today.