0

What does it mean for a variable to be declared but not defined? I've noticed that this can happen in node repl and in the browser console.

When declaring a variable fails, the variable is left declared, but not defined. Attempting to redeclare the variable fails, but it also fails to define the same variable.

$ node
Welcome to Node.js v12.16.2.
Type ".help" for more information.
> let x = test;
Uncaught ReferenceError: test is not defined
> let x = 'test';
Uncaught SyntaxError: Identifier 'x' has already been declared
> x = 'test';
Uncaught ReferenceError: x is not defined
> x
Uncaught ReferenceError: x is not defined
>
Benjamin
  • 3,428
  • 1
  • 15
  • 25
  • Scope can be odd when typing into the console directly. Basically, since `let x` exists in the code, other declarations of `x` aren't allowed, but since the right side of the `let x =` failed, the variable never got properly initialized, so it can't be assigned to later either. – CertainPerformance Nov 05 '20 at 20:03
  • @CertainPerformance thank you. I found the answer here: https://stackoverflow.com/questions/54979906/is-there-any-difference-between-declared-and-defined-variable – Benjamin Nov 05 '20 at 20:05

0 Answers0