Is there any difference between declared and defined variable

I try writing the following lines in the console one by one

let x = y //throws error "Uncaught ReferenceError: y is not defined"
console.log(x) //throws error "ReferenceError: x is not defined"
let x = 3; //gives error "Uncaught SyntaxError: Identifier 'x' has already been declared"
x = 3 //ReferenceError: x is not defined

Now problem is that how can be a variable not defined and has been declared at the same time. Is there any difference between both.



A let or const variable can only be declared once - that is, when you have let <variableName> in a scope, you have declared <variableName> in that scope, and cannot declare it again in that scope.

From the previously linked question:

When there's assignment, the right-hand side is parsed first; if the right-hand side throws an error, it never gets to the left-hand side, and the variable declared with let never gets properly initialized; it'll stay in the demilitarized zone / temporal dead zone forever

You can't re-declare a variable that's already been declared, even though the attempted assignment during initialization threw an error.

But on line 4, x=3 should do a proper assignment and it should remove x from TDZ. But that also fails. I fail to understand that

After a variable has been initialized (for example, the let x runs), it can be assigned to. But just like you can't assign to a variable before its let initialization, you also can't assign to a variable later, when its initialization did not complete successfully:

x = 'foo';
let x = 'bar';


Uncaught ReferenceError: x is not defined

Which is the same sort of thing that happens in the console when you try:

let x = y
// Uncaught ReferenceError: y is not defined
// x has not been initialized, so the next line throws:
x = 'foo'
// Uncaught ReferenceError: x is not defined

x still has not been initialized, so the error is the same.

Encountering this sort of thing is pretty odd, though - you only see it in the console. In normal scripts, a thrown error will prevent further execution, and the fact that a variable name remains uninitialized forever is not something to worry about.

The above was an issue in earlier Chrome versions. But in Chrome 80+, re-declarations of let are now permitted, so the error

Uncaught SyntaxError: Identifier 'x' has already been declared

should no longer occur, regardless of whether the previous initialization of the variable succeeded or not:

enter image description here


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.