ECMAScript 2015 Temporal Dead Zone [duplicate] - javascript

This question already has answers here:
What is the temporal dead zone?
(3 answers)
ES6 typeof throws an error
(1 answer)
Closed 6 years ago.
I am new to ECMAScript 2015(ES6) and I am reading about temporal dead zones in ES6:
if(typeof x === "undefined") {
console.log("x doesn't exist or is undefined");
} else {
// safe to refer to x....
}
let x = 5; //script.js:1 Uncaught ReferenceError: x is not defined
Obviously in ES6 if you test a variable with typeof before declaring it throws errors
console.log(typeof x);
let x = 5; //script.js:1 Uncaught ReferenceError: x is not defined
Why is this happening? Is this a bug?

That's the way it goes:
Temporal dead zone and errors with let
In ECMAScript 2015, let will hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.

Related

Need clarity regarding LET and temporal dead zone [duplicate]

This question already has answers here:
What is the temporal dead zone?
(3 answers)
Closed 10 months ago.
console.log(a);
let a = 10;
var b = 100;
Output:
script.js:1 Uncaught ReferenceError: Cannot access 'a' before initialization
let a;
console.log(a);
a = 10;
console.log(a);
Output:-
undefined
10
Doubt:-
While performing the first console.log(a), a is in temporal dead zone since it hasn't been initialized. So why is it not throwing any error and printing undefined?
Because you have defined the variable but not the content
in the first example the variable is defined after is called so it throw an error
in the second case the variable is defined but it's content doesn't.
So the first console.log print the content undefined

Why does it throws reference error for b? [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Recently I attended a interview for Frontend development, The interviewer came up with a problem which made me doubt my confidence on javascript. The question is
function some() {
console.log(a) // undefined
console.log(b) // Reference Error: cannot access b before initialization.
var a = 10;
let b = 15
}
I understood hoisting is happening here, but i am not sure why b throwed reference error. Could anyone please explain ?
Unlike variables declared with var, which will start with the value undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Why this give Uncaught ReferenceError [duplicate]

This question already has answers here:
What is the temporal dead zone?
(3 answers)
Closed 3 years ago.
the expected output as far as I understand must be :
10
22
let y = 10;
{
console.log(y); // this line gives refrence error
let y = 22;
console.log(y);
}
{
console.log(this.y); // this line gives refrence error
let y = 22;
console.log(y);
}
You are trying to call a variable that is not declared yet. Thats why you're getting this error. Your first console.log is not looking at the global scope ,it's looking at the block scope. And at the time you're calling it, it's not defined yet. A " var" would've returned undefined. let and const return this error. I hope it's clear.
You can test this yourself using 'this'. as you can see this.y is undefined.

Unexpected behaviour of JavaScript with let and var keyword [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Are variables declared with let or const hoisted?
(7 answers)
Closed 4 years ago.
When i am using var keyword to declare a variable then JS engine is assign default value to "message" at creation stage
console.log(message); //undefined
var message = "My message";
but with let keyword
console.log(message); //message is not defined
let message = "My message";
why this unexpected result or it is something changed in ES6?
This is because of the creation of Temporal dead zone with let
let bindings are created at the top of the (block) scope containing the declaration, commonly referred to as "hoisting". Unlike variables declared with var, which will start with the value undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.

Understanding let vs. var hoisting [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
Closed 6 years ago.
With let vs. var I've learned that the major difference is that let variables are scoped to the nearest block and are not hoisted. Also let variables can be reassigned but cannot be redeclared within the same scope. Why then does this code return a "not defined" error?
let x = 10;
if (true) {
console.log(x);
let x = 11;
}
returns:
Uncaught ReferenceError: x is not defined(…)
While:
let x = 10;
if (true) {
console.log(x);
}
logs 10 without an error?
The main difference between var and let is that:
var is hoisted to the wrapping function block.
let is hoisted to the wrapping {} block
The second code sample doesn't have the same reference conflict as the first because you're declaring x prior to referencing it in the following if block.
EDIT To address Pointy's comment below:
You're experiencing the ReferenceError because of the temporal dead zone
If you reference a variable defined by let within the same block before it is defined you will receive this error.
From MDN let Docs
In ECMAScript 2015, let will hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.
function do_something() {
console.log(foo); // ReferenceError
let foo = 2;
}
let variables are actually hoisted (within the block). From MDN:
In ECMAScript 2015, let will hoist the variable to the top of the block.

Categories

Resources