0

I understand from MDN that 'undefined' is recognised as a primitive value, which is corroborated by the ES doco also stating that an "undefined value" is a "primitive value used when a variable has not been assigned a value".

I also understand even though the variable may not be assigned a value (i.e. an uninitialised variable), memory is still allocated for it during the creation of its execution context ('creation' phase) prior to execution happening. This explains why when we attempt to access the variable, we do not get a reference error - rather we just encounter 'undefined' (i.e. in the console log).

Noting the above, my question is, in memory, what does this look like at the memory location of the variable? Is there actually a value at the allocated memory location/address of the undefined variable? Or is the value at the memory address empty (nothing there)? If so, could we then describe the value as null (0x00)?

Thanks.

trincot
  • 317,000
  • 35
  • 244
  • 286
151SoBad
  • 115
  • 9

1 Answers1

2

I also understand even though the variable may not be assigned a value (i.e. an uninitialised variable), memory is still allocated for it during the creation of its execution context ('creation' phase) prior to execution happening. This explains why when we attempt to access the variable, we do not get a reference error - rather we just encounter 'undefined'.

No. An uninitialised variable is something different than a variable initialised with the value undefined. Have a look at this answer explaining the initialisation for various kinds of declarations.

What does this look like at the memory location of the variable?

It doesn't really matter how this is implemented. Every implementation may do it different, what matters are the observable effects in JS.

Is there actually a value at the allocated memory location/address of the variable, or is the value at the memory address empty?

Allocated memory always holds some value. It might be a value for which no representation exists in JS, though.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Interesting, thanks. Your first comment however, and the link to that thread where you talk about how var declarations are initialised to undefined when the binding is created at the top of the scope. Isn't that exactly what I've said here, I don't get your point "An uninitialised variable is something different than a variable initialised with the value undefined"? var greet; console.log(greet); // undefined var hello = undefined; console.log(hello); // undefined console.log(greet == hello); // true console.log(greet === hello); // true They seem equal (not 'different') to me? – 151SoBad Sep 03 '17 at 04:53
  • @151SoBad Try with `console.log(greet); let greet;` - *that*'s uninitialised. A `var` is always initialised with the `undefined` value at the top of the scope. – Bergi Sep 03 '17 at 09:08
  • Makes sense - I haven't caught on to using 'let' yet - still very vanilla Javascript. One last thing I need to ask, someone commented on your post stating "for function declarations, which are assigned a value before execution begins", this was the same concept that has been bothering me. The last time I had a look at the ES specs I thought function declarations are assigned a value only during execution? Is assigning a value to function declarations occurring during "declaration binding instantiation" (verbatim from ES6 specs)? This occurs during execution doesn't it? – 151SoBad Sep 03 '17 at 11:26
  • @151SoBad Function declarations are "hoisted", which means the variable is initialised with the value right when the variable is created at the top of the scope. – Bergi Sep 03 '17 at 12:17
  • So if u had a variable expression (using 'var'), i.e. var x = 5, the VARIABLE declaration (var x) is HOISTED, AND its value initialised to/with UNDEFINED (and it is only during execution that the variable is set to 5). WHEREAS FUNCTION declarations are HOISTED AND they are initialised to/with a VALUE (that is NOT UNDEFINED) (which is converse to variable declarations)? What is this value? A reference to the function object? If so, that's why I'm confused - I thought the function object is only instantiated during execution, not prior to execution (similar to how hoisting is prior to execution) – 151SoBad Sep 03 '17 at 13:08
  • @151SoBad Yes, their value is the function object. That's why you can call it above the declaration: `example(); function example(){…}`. – Bergi Sep 03 '17 at 13:17
  • I see. Thanks for clarifying that - that's the same rationale I had in mind. I'm sure this is also stated somewhere in the ES spec and perhaps I misinterpreted it as only being instantiated during execution. So although the function object is instantiated, clearly the function arguments/parameters would not be instantiated? Rather, would it be creating/instantiating on that function object perhaps the internal 'code' property (which has a reference to the code in the function body) AND the function declaration variable name? – 151SoBad Sep 03 '17 at 16:21
  • @151SoBad Yes, the variables for the parameters are instantiated when a function is *called*, not when it's created. – Bergi Sep 03 '17 at 17:48
  • Thanks Bergi. And any comment on my last sentence on the above comment there? – 151SoBad Sep 03 '17 at 19:50
  • @151SoBad Yes, function objects are created with all kinds of internal properties. – Bergi Sep 03 '17 at 20:47
  • Appreciate that Bergi - this community is fortunate to have people like yourself. Thanks. – 151SoBad Sep 03 '17 at 21:02