8

I always wondered how JavaScript stores null values internally. null is different than any value. I imagine that the scope's variables are stored in some kind of array of structures, each structure corresponding to a variable. Does this structure has some kind of boolean property called "null"?

I would look for it myself in the V8 source code, but am am pretty lost in the C++ code :(

I couldn't find anything on Google. Most results were related to questions like "How to determine if variable is undefined or null?" or similar.

David G
  • 94,763
  • 41
  • 167
  • 253
Aalex Gabi
  • 1,525
  • 1
  • 18
  • 32
  • 3
    It probably depends on the interpreter, though I'd be surprised to learn that it was anything much different than simply zero. It's not really an important detail. – Pointy Dec 27 '12 at 00:24
  • 1
    Keep it mind that there are several "falsy" values: null, undefined, false, 0, etc. More likely, there is a "type" field on each variable that indicates whether the value is null, undefined, a number, a boolean, an object, etc. – cdhowie Dec 27 '12 at 00:25
  • @Pointy Sure, it depends on the interpreter. If it would store null values as zero then why `(null === 0)` returns `false`? – Aalex Gabi Dec 27 '12 at 00:27
  • 1
    @AalexGabi Because the *internal* representation doesn't need to be exposed to the JS developer. – Dave Newton Dec 27 '12 at 00:28
  • "`null` is different than any value" - so is `17`. It's a unique arrangement of bits, dependent on the engine. There's nothing special about `null`, as opposed to, say, `true` or `{ a: "b" }`. And unless you write C++ code (i.e. use V8 embedding), you never need to know it. Maybe not even then. – Amadan Dec 27 '12 at 00:30
  • @AalexGabi the interpreter could make `null` look like anything it wanted. So long as the semantics of `null` are preserved in JavaScript, it makes absolutely no difference what it is. – Pointy Dec 27 '12 at 00:30
  • As it happens one of the main developers of Guile has done some analysis on JavaScript interpreters and blogged his finds. See: http://wingolog.org/tags/javascript (It includes among other things lots of details on the guts & gore of V8, including JavaScript value representation.) – user268396 Dec 27 '12 at 00:33
  • @DaveNewton When I run `var container = null; console.log(container === 0);`, it prints `false`. How the interpreter makes the difference between integer zero and `null`? – Aalex Gabi Dec 27 '12 at 00:34
  • @AalexGabi However it wants: again, the *internal representation* has absolutely *nothing* to do with how *JS is exposed to the JS developer*. `null` is a keyword. *Any* code could be run during interpretation, compilation, JITting, etc. to handle the `null` keyword. – Dave Newton Dec 27 '12 at 00:36
  • @Amadan I agree with you. I don't need to know in order to use it. Is just that I was always intrigued by this! – Aalex Gabi Dec 27 '12 at 00:47

1 Answers1

4

JavaScript, being a weakly-typed scripting language, probably has a variant type that supports values of all kinds of types. I believe PHP uses the same type of variables, although they are known in other languages too and play an important role in COM automation.

They are basically structs that contain a type and a value. The value can be a simple value, or a pointer to a larger or more complex value. null is just one of the supported types, in which case the value itself plays no part.

But since JavaScript knows many interpreters, there is no rule about how it is implemented, and it is something you should not worry about. Normally you would only need to know about underlying techniques like this for time-critical applications, which should not by written in JavaScript anyway.

David G
  • 94,763
  • 41
  • 167
  • 253
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 2
    `null` is not a separate type, really; it's the empty value for `Object` reference values. – Pointy Dec 27 '12 at 00:30
  • I was talking about the implementation in the commonly know variant records. Javascript may describe `null` as a special type of object, but I highly doubt that internally there actually is an object. – GolezTrol Dec 27 '12 at 00:33
  • 1
    @Pointy (Although I was a little surprised that `typeof(null)` returns `"object"`.) – Dave Newton Dec 27 '12 at 00:33
  • @DaveNewton Good point. Javascript itself seems to think it is an object. ;) – GolezTrol Dec 27 '12 at 00:35
  • @DaveNewton well it's a reference to no object; it's not a number, or a string, or a boolean, so if it's not `undefined` the only other thing it can be is `object`, which really means "reference to an object". – Pointy Dec 27 '12 at 00:38
  • 2
    Note that `null === null`, so it's as if there's a singleton instance of the `null` value. – Drew Noakes Dec 27 '12 at 00:39
  • Thank you all for your answers. Now it's clear to me that `null` is stored as some kind of variable metadata (or rather value metadata). – Aalex Gabi Dec 27 '12 at 00:57
  • 1
    Probably worth repeating; this blog post explains how JavaScript values work in some detail: http://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations – user268396 Dec 27 '12 at 01:05