3

In this situation var foo;

Will "foo" point to somewhere in the memory that contains the value undefined?

Does undefined occupy memory in javascript? Does undefined mean that there is no memory address assigned to a specific label?

Haitham6373
  • 1,139
  • 8
  • 10

3 Answers3

4

There's two parts to your question. The variable itself and multiple copies of undefined value.

First, variables can take space in memory. It's not always known if they do take space since compilers can optimize your code in a way that they might not do so (or even straight up removed).

As of the other part of your question. Per the specification there's only one undefined value but the specification does not require the implementation to hold a unique representation of undefined in memory. So any implementation can hold multiple copies of the value so long as the language semantics can be guaranteed.

Edit: Also, the specification does not specify its value representation in memory, so that's also an implementation detail.

MinusFour
  • 13,913
  • 3
  • 30
  • 39
  • When JS is defining the scopes (parsing), it will search for declarations, right? for example ` var foo = 2; ` it will see it as two parts the first part `var foo; `, and then later at the execution time, JS Engine will set the value to it, like this `foo = 2; `. So now, the variable has been set twice, the first time to "undefined", and then to the number 2. What if we used `const` instead. const can't point to a new place in the memory. so, we can't set it twice? What am I missing here? – Haitham6373 Aug 21 '21 at 14:09
  • 1
    @Haitham6373 You seem to refer to [hoisting](https://stackoverflow.com/a/32475965/1048572), and no, that's not really [how it works for `const`](https://stackoverflow.com/q/31219420/1048572). – Bergi Aug 21 '21 at 14:10
  • 1
    `const` is never initialized to `undefined` unless you explicitly do so. `let` and `const` are not initialized until they reach their respective declarations and `const` always require an initializer. `const foo;` is an error. – MinusFour Aug 21 '21 at 14:18
3

Any kind of variable you create will occupy memory space, regardless if it was created with let, const or var. When you do it, you're literally telling JS to reserve memory space because you will store values in that variable. undefined is a value in JavaScript, therefore, yes, it does consume memory.

There are tests regarding performances between let and var that leads to some discussion about which is faster (see this stack post), and const is recommended for values you have that won't change.

Pelicer
  • 1,348
  • 4
  • 23
  • 54
  • "*tests that prove that `let` is faster than `var`*" - I don't see any claims of that in the thread you linked. And a single benchmark doesn't "prove" anything. If you'd just remove that sentence from your post, it would be the perfect answer. – Bergi Aug 21 '21 at 14:07
  • 1
    You're correct, my mistake. I've updated the answer. – Pelicer Aug 21 '21 at 14:09
1

Any variable you declare in javascript will be assigned to a memory location. if you don't initialize the variable while declaring, then it will be assigned to undefined by Javascript. which means you declared a variable and it's not been assigned yet.

undefined is a value in javascript. You can read here

Mahesh
  • 355
  • 1
  • 12
  • When JS is defining the scopes (parsing), it will search for declarations, right? for example ` var foo = 2; ` it will see it as two parts the first part `var foo; `, and then later at the execution time, JS Engine will set the value to it, like this `foo = 2; `. So now, the variable has been set twice, the first time to "undefined", and then to the number 2. What if we used `const` instead. const can't point to a new place in the memory. so, we can't set it twice? What am I missing here? – Haitham6373 Aug 21 '21 at 14:11
  • there is a concept called hoisting in javascript, in this concept the `variable declarations` will be moved to the top before `execution`. so, this is the same for all types of variables regardless of whether it is `var`, `let`, and `const`. But while using `const` you can't skip assigning value to it. that's why you don't experience hoisting. You can read more about hoisting https://developer.mozilla.org/en-US/docs/Glossary/Hoisting – Mahesh Aug 21 '21 at 14:51