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.
Related
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
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]
}
This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 4 years ago.
I noticed a bug in my code a couple days ago and have no idea why it's happening. It seems a variable defined in a lower scope is somehow jumping up to a higher one. Anyone know what's happening here? Dumbed down code:
console.log(a)
for(var k = 0; k < 5; k++)
var a = 5
console.log(a)
The first console log always prints undefined
But the second console log always prints 5?
Shouldn't variable a only exist in the for loop's scope and be cleared from memory once the for loop is done?
Variables defined with var are "function scoped", so they are accessible anywhere in the function. let and const however have "block scoping", they will behave like you expect:
{
let a = 1;
var b = 2;
}
console.log(a, b); // not defined, 2
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
In the following code, shouldn't x be considered a global variable? Thus, when it gets to the console.log line x it should have the value of "World". However, when I run this code it logs "Hello undefined". `
let x = "World";
function sayHello(x) {
console.log("Hello ", x);
}
sayHello();
But when I change the parameter to y, it then works as I expected, logging "Hello World.
let x = "World";
function sayHello(y) {
console.log("Hello ", x);
}
sayHello();
Can someone explain what is going on here?
thanks
The x parameter in sayHello(x) shadows the global let x = "World";. If you pass a value in, sayHello("Hello"), you would get an output, Hello.
Try calling sayHello(‘Stack Overflow’). I’m sure you can guess whet the result will be.
The function is expecting x to be the first argument passed to the function. If no argument is passed, the argument x is undefined.
The technical term for what happened here is shadowing. The argument named x shadows the variable named x, even if no value is passed for the argument.
This question already has answers here:
Why is no ReferenceError being thrown if a variable is used before it’s declared?
(3 answers)
does this variable get hoisted no matter what?
(1 answer)
Closed 5 years ago.
I have a question about following code blocks
var x = "123" + y;
in this code block I am getting
Uncaught ReferenceError: y is not defined
but when I try something like this
var x = "123" + y;
var y = "456";
It works fine, so why? What is the difference?