0
  • Executing an undeclared variable x gives error: Uncaught ReferenceError: x is not defined
  • Accessing a variable before declaration with let gives similar error: Uncaught ReferenceError: y is not defined
console.log(y)
y=10;
let y;

As we see that both cases throws similar error but we know that. So, how to prove that variable hoisting do happen with let or const declaration?

enter image description here

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    The Chrome console is not the best way to "prove" anything. It does not behave exactly the same as executing a script. Run this JS *properly* and it will not say "y is not defined" but complain about the temporal dead zone. Which then proves there is hoisting. – VLAZ Aug 19 '22 at 12:28
  • Hi folks, kindly help me to re-open this question as it is about "how to prove" the fact of variable hoisting in let declaration and not on the topic itself. Proving an existing topic is unique and not duplicate. – Shammi Hans Aug 19 '22 at 12:54
  • Why is proving "needed"? What is the *practical* problem to be solved here? The duplicate already has reference to the specification which *requires* this behaviour. That seems like the best proof. Moreover, your question here can only come up if you use the Chrome devtools console. Which is *not* in any way a good proof of any behaviour. The Chrome console can *and does* things that are not part of the usual code processing. For example using `{} + {}` give *the wrong result* for evaluating the code. Because it evaluates it *as if* it was `({} + {})` and that's not the same code. – VLAZ Aug 19 '22 at 12:59
  • @VLAZ I agree to your point that using Chrome devtools console will throw a different error. But the point is, if we understand a concept properly we should be able to prove that, hence proving is necessary. If this topic gets re-opened people will give their views. I/others may learn something new. In case my approach is wrong feel free to down-vote the answer but at least help me to re-open a genuine question. There isn't a question of proving that fact. – Shammi Hans Aug 19 '22 at 13:28
  • The point is that if you aren't using the Chrome console, there is nothing that needs to be proven. The result is `ReferenceError: Cannot access 'y' before initialization` which means the declaration was hoisted. I do not see 1. What use is there for this question. It doesn't seem like a practical problem to solve. It's entirely constructed and should be a non-issue outside irrelevant contexts. 2. Why would it be on-topic. as it seems like it's seeking more discussion than a Q&A format. 3. Why the spec *and observable behaviour* as described in the duplicate is not enough proof. – VLAZ Aug 19 '22 at 13:40
  • @VLAZ yeah you got it right, [that and all similar explanations]( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init) isn't enough, and not at all focused on the details of how can we prove variable hoisting, there it is just mentioned 2 things: hoisting occurs and accessing before initialisation will throw that error. – Shammi Hans Aug 19 '22 at 18:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247404/discussion-between-shammi-hans-and-vlaz). – Shammi Hans Aug 20 '22 at 06:21

1 Answers1

0

I know just one way to prove that variable hoisting do happen with let or const despite of same Uncaught ReferenceError.

  1. if variable is hoisted in let or const declaration, the variable must have been declared, that means it has already allocated a memory space
  2. if we can prove that the variable has occupied a memory space before accessing it (rather before code execution), that means the variable is hoisted

We can use a browser dev-tools to check for the same, let us add a debugger in the first line to pause the execution before accessing the variable declared with let

debugger;
console.log(y)
y=10;
let y;

When this is executed in console tab, it redirects to the source tab where we can see the variable y already allocated a memory space that's why it is shown under Script in the Scope section on the right side (denoting lexical scope of let declaration for variable y)

enter image description here

However, this will throw the same error: Uncaught ReferenceError: y is not defined in line 2, since it is not defined.

But the point is we have proved that the variable has allocated a memory space before execution, hence variable hoisting do occur in let or const declaration

Let me know your views. I would like to know more ways to prove the same. So please feel free to add your points.