2

Javascript novice here. From https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/ :

  • They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.
  • While var and let can be declared without being initialized, const must be initialized during declaration.

So "const" is clear that it's initialized as the value it was originally declared as.

"var":

a) not declared, initialized as undefined
b) declared, initialized accordingly

"let":

a) not declared, initialized as ______???______
b) declared, initialized accordingly

What is "let" initialized as if it's not declared at first?

monkeDB
  • 21
  • 2
  • "*What is "let" initialized as if it's not declared at first?*" nothing. It's unusable. If you try to read or write the variable, you'll get into [the temporal dead zone](https://stackoverflow.com/questions/33198849/what-is-the-temporal-dead-zone) and you'd get an error. See also: [Are variables declared with let or const hoisted?](https://stackoverflow.com/q/31219420) – VLAZ Aug 18 '21 at 07:01
  • Note that although "variables declared with let and const are also hoisted", "until the line in which they are initialized is executed, any code that accesses these variables will throw an exception". https://developer.mozilla.org/en-US/docs/Glossary/Hoisting – kol Aug 18 '21 at 07:04
  • My advice: 1) Do not ever use `var`. Refactor it out of exisitng code if possible. 2) Always use `const` if the value won't change. For example, if you create an array to push elements into it later, the array still can be `const` because its a reference that won't change. 3) Use `let` only if you are sure the variable will change. For example, for loop indexes. 4) Put `const` and `let` declarations as close to where they are used as possible, just like in other programming languages, and forget about their hoisting. – kol Aug 18 '21 at 07:11
  • (cont.) If some code would try to use them before the declaration, you will get an error at runtime. 5) Use JSDoc type hints or TypeScript. JSDoc can do wonders in editors like Visual Studio Code, because the editor will show you every type-related errors, even in pure JavaScript code. Also the editor will show when a value is used before declared, so you won't have to think about hoisting at all. – kol Aug 18 '21 at 07:14

3 Answers3

1

let if declared and not initialized, then its default value is set to undefined.

let x;
console.log(x);

>> undefined
0

Well, the easiest way to think about it is that there is only 1 type of undeclared variable (in the sense of never declared, otherwise you just get a ReferrenceError). If it is never declared, the JavaScript engine can't be choosing to make it a var or a let or a const to fit the developer needs.

Any never declared variable that is assigned a value becomes a global scope variable (ref: comment from Avikalp Gupta)

Sirode
  • 379
  • 1
  • 2
  • 13
  • Undeclared variables are different from `var` variables. They have global scope, unlike `var` variables. Refer: https://avikalpgupta.medium.com/var-let-const-and-undeclared-variables-in-javascript-32d3c72442ba – Avikalp Gupta Feb 07 '22 at 18:29
0

Hoisted let variables are initialized with a special "value" that throws a ReferenceError when accessed:

function foo() {
    console.log(x)  // ReferenceError: Cannot access 'x' before initialization
    let x
}

foo()

let and const declarations define variables that are scoped to the running execution context's LexicalEnvironment. The variables ... may not be accessed in any way until the variable's LexicalBinding is evaluated.

https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-let-and-const-declarations

georg
  • 211,518
  • 52
  • 313
  • 390