How to prove that variable hoisting occurs with let declaration? [duplicate] - javascript

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
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?

I know just one way to prove that variable hoisting do happen with let or const despite of same Uncaught ReferenceError.
if variable is hoisted in let or const declaration, the variable must have been declared, that means it has already allocated a memory space
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)
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.

Related

Hoisted JavaScript variables undefined or not? [duplicate]

This question already has answers here:
Using Javascript hoisting concept , I tried the following. I am not sure why the name variable has been picked up but not the age [duplicate]
(1 answer)
What is the global variable called 'name' in javascript? [duplicate]
(2 answers)
Closed last month.
I am trying to understand in a more deep way hoisting, creation phase and execution phase in JavaScript. To the best of my knowledge variables declared with var keyword are hoisted to the top of their scope. But I am running into this problem.
console.log(name); // Output -> 'any name'
console.log(age); // Output -> undefined
var name = 'any name';
var age = 30
I know at the moment when console.log statements are executed bot variables should be undefined because they are declared but values have not been assigned to them (they got undefined in the creation phase). However browsers keep giving me one of the values as undefined (as I expect it to be), but with name variable it is reading the string. Any ideas as to why is this happening?

Why is variable undefined in global object even though it has not been defined? [duplicate]

This question already has answers here:
ReferenceError and the global object
(3 answers)
Closed 1 year ago.
I have this code:
console.log(apple);
If I run the program, I get the following result:
console.log(apple);
^
ReferenceError: apple is not defined
This is what I expected, but if I modify the code to print the apple variable from the global object, I get undefined
console.log(global.apple);
Result:
undefined
How is this undefined? As per my understanding, this should give the ReferenceError too right? Sorry if this is a simple concept, I am trying to understand the fundamentals. Any reference would be helpful.
What you're forbidden to do is reference a standalone identifier that the interpreter cannot resolve.
Referencing a property of an object - even if the property doesn't exist - is perfectly fine. It will result in undefined, but it won't throw.
const obj = {};
console.log(obj.foo);

What if we console a variable just before declare it [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 4 years ago.
What is the difference between the following two cases:
CASE 1:
console.log(c); //console : undefined
var c;
CASE 2:
console.log(c); //console : Uncaught ReferenceError: c is not defined
Why in Case 1 it console undefined? as it was declare after console.log(). At that time variable c is also not defined than why program console undefined
Please check docs for var statement in MDN
variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.
So, in first example variable has been declared, but was not defined (even with var c = 1; before that row it would be not defined). In the second example, it was not declared at all.

How do we explain javascript hoisting here? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Why is no ReferenceError being thrown if a variable is used before it’s declared?
(3 answers)
Closed 5 years ago.
I wonder why the two code snipped below generate two different results as in reference below the two have to be the same as were are in global scope and placing var would not make a difference.
I was under the impression due to what explained below and the Javascript hoisting the two generate same output, but don't! why?
What is the purpose of the var keyword and when to use it (or omit it)?
console.log(a)
var a = 90;
// undefined
vs
console.log(a)
a = 90; // no var keyword
// Uncaught ReferenceError: a is not defined
When you put var keyword, what it does is it moves the var part as a declaration on that scope and makes it undefined. In the first case, with var:
console.log(a)
var a = 90;
// undefined
Gets rewritten as:
var a = undefined;
console.log(a)
a = 90;
// undefined
So, you get the undefined here, vs. your second case has it the same way, where there's no declaration or key in window object named a. Hope this is understandable.
The point here is, when you use a var keyword, immediately that's defined as undefined at the top of the scope before anything.

Using an if statement with a let/const statement, without a block statement [duplicate]

This question already has an answer here:
let in if without braces
(1 answer)
Closed 6 years ago.
In JavaScript, I can declare a variable using var in an if statement, without a block statement:
if (true)
var theAnswer = 42
However, trying to declare a variable using let or const without a block statement gives an error:
if (true)
let theAnswer = 42
Chrome throws SyntaxError: Unexpected identifier, Firefox—SyntaxError: lexical declaration not directly within block.
if (true)
const theAnswer = 42
Here Chrome throws SyntaxError: Unexpected token const, Firefox—SyntaxError: const declaration not directly within block.
What is the reason of that? Is there anything in the specification that would explain this behavior?
It would be a foot-gun with little to gain. var initialization is hoisted, so the variable is guaranteed to always exist inside the function and always have a value. let and const are scoped within blocks/functions and initialization is not hoisted. What this means is that if the case you present was allowed, code like the following would throw:
if (false) let foo = 4;
console.log(foo);
Because the let foo line never executes, the foo variable would never be initialized. That means that accessing the foo variable would always trigger a "temporal dead zone" error, just like
console.log(foo);
let foo = 4;
would.
By disallowing let without a direct block/function wrapper, let would be dangerous and provide minimal gain.

Categories