-2

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?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Magnus
  • 6,791
  • 8
  • 53
  • 84
  • You might find the information [in this question's answers](https://stackoverflow.com/a/13266769/215552) interesting, but what is the practical reason you need to know? – Heretic Monkey Mar 18 '18 at 18:54
  • v8 is pretty complicated and I don't think there's one simple answer. It looks like [here is](https://github.com/v8/v8/blob/master/third_party/inspector_protocol/lib/Values_h.template) one representation of javascript values in the v8 source, though I can't tell whether it's a particularly relevant one. – Chris Martin Mar 18 '18 at 19:27
  • 1
    I don't understand. How does this question about JavaScript have any relationship to C++? I recommend removing the C++ tag. – Thomas Matthews Mar 18 '18 at 20:05
  • Could the downvoters / closer please provide information on why the question was treated in this manner? It is a fair question, one that I have not been able to find an answer to. I am trying to understand how the engine knows what type a value is. Is it in a few bits before the value itself, that tells the engine what the value is? – Magnus Mar 19 '18 at 00:56

2 Answers2

1

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thank you. I don't understand why it is voted down. It is an interesting piece of information, which I was not able to find the answer to elsewhere. – Magnus Mar 19 '18 at 01:00
0

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.

Sebastian Speitel
  • 7,166
  • 2
  • 19
  • 38