Need clarity regarding LET and temporal dead zone [duplicate] - javascript

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

Related

output of the javascript snippet? javascript variable declarations [duplicate]

This question already has answers here:
How JS hoisting works within functions?
(4 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed last year.
My first guess is that the following code has the output of 10 and 100. However, after running the code I get undefined and 100 and I don't understand why the function doesn't see the first declaration of x..
var x = 10;
function f() {
console.log(x);
var x = 100;
console.log(x);
}
f();

Doing typeof variable throws an error when used inside a for loop [duplicate]

This question already has answers here:
What is the temporal dead zone?
(3 answers)
Are variables declared with let or const hoisted?
(7 answers)
Closed 2 years ago.
For some reason when I try to access the rates variable inside the for-in loop, it throws a ReferenceError, but I can use it just fine right before the loop. I can't seem to figure out what is causing this behavior. Even doing typeof throws this error, but it works fine on a variable that was never used at all:
const test = 1;
const rates = await Rate.find({
propertyId: property._id,
ratePlanId: {$in: ratePlanIds},
});
console.log(test); // 1
console.log(typeof rates); // object
for (const i in ratePlans) {
console.log(test); // 1
console.log(typeof asdlkfjasdajsfl); // undefined
console.log(typeof rates); // ReferenceError: rates is not defined
//[... rest of code]
}

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.

ECMAScript 2015 Temporal Dead Zone [duplicate]

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.

Categories

Resources