-1

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);
}
  • What are you trying to do – Kevin.a Jun 19 '19 at 11:42
  • I have a global variable I want to use in the inner block {} after that I want to make a local variable with the same name but this gives error, I want to understand how javascript handles this – Doaa Ismael Jun 19 '19 at 11:43
  • It's probably how JS is evaluated, the interpreter figured you are referring to the variable that you declared within the block before it was declared. – Gaurav Punjabi Jun 19 '19 at 11:44
  • @DoaaIsmael Try using `var` then. – Praveen Kumar Purushothaman Jun 19 '19 at 11:44
  • *"the expected output as far as I understand must be : 10 22"*. This would be hard to decipher, wouldn't it ? Temporal dead zones have been designed exactly to avoid this kind of code going into production. – Denys Séguret Jun 19 '19 at 11:44
  • let y = 10; { console.log(y); // this line gives refrence error y = 22; console.log(y); }. – Kevin.a Jun 19 '19 at 11:46
  • I know it's not a good practice, but it's just for learning purposes to know how the interpreter work! – Doaa Ismael Jun 19 '19 at 11:46
  • You might want to see also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block - `let` variables have "block scope". So because you declared a second `y` within your `{ ... }` block, the first `y` cannot be used within that block. This is true even though you didn't write the declaration of the second `y` until later in the block. The scope extends throughout the whole block, not just the part after you declared it. The dead zone concept is there to force you to write more logical code by declaring variables before you try to use them – ADyson Jun 19 '19 at 11:50
  • ...cont'd: And just to expand on the scope explanation a bit, if you had simply written `let y = 10; { console.log(y); }` then it would work, because within the bracket block there is no other variable with the same name to override the outer one. So your expectation that the first log() command should output 10 is incorrect, because the variable you expected the value to come from does not exist within your block - it has been superseded by the variable with the same name you declared inside the block. – ADyson Jun 19 '19 at 11:50
  • @ADyson, yes, I got it. thank you so much – Doaa Ismael Jun 19 '19 at 12:01

1 Answers1

-1

{
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.

Kevin.a
  • 3,482
  • 4
  • 28
  • 65
  • first one is in the global scope and the second is in a block scope. they are not the same. if they are the same, it would give already defined Error – Doaa Ismael Jun 19 '19 at 11:50
  • I am sorry but this is either a very low effort answer or you simply are lenient with your usage of terms -- "Let is already declared before"? "if you want to re-declare you can simply use `y = 22`"? "It tries to call the let"? Is English not your first language, just curious? I can edit the answer for you, if you want. – amn Jun 19 '19 at 11:51
  • @amn English is indeed not my first language, feel free to edit my answer. ^^ – Kevin.a Jun 19 '19 at 11:52
  • On second thought, it would be very difficult for me to edit your answer and try to retain exactly what you're trying to communicate. – amn Jun 19 '19 at 11:55
  • @amn i have decided to edit it myself. I agree my first effort wasnt all too good. I hope its better – Kevin.a Jun 19 '19 at 11:57
  • @DoaaIsmael is this clear for you? – Kevin.a Jun 19 '19 at 11:57