Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 344
Maheer Ali Avatar asked Mar 04 '19 09:03

Maheer Ali


People also ask

What is the difference between defining and declaring a variable in C?

Difference Between Definition and Declaration It aims at specifying the name of any given class, function, variable, etc. Definition allocates memory to an entity. A declaration does not allocate memory to the entities. Once you define an entity, you cannot keep repeating the definition process again and again.

What is the difference between declaring and defining a function?

The main difference between Function Declaration and Function Definition in C Programming is that Function declaration indicates what the function is and Function Definition indicates what the function does.

What is the difference between declaring a variable and defining a variable in Java?

The declaration could be done multiple times either of a variable or of function. Variable or function could be defined only once. Memory has not been allocated during the declaration of a variable or function. Memory has been allocated during the definition of a variable or function.


1 Answers

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';

Error:

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

like image 134
CertainPerformance Avatar answered Oct 28 '22 14:10

CertainPerformance