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?

4
  • "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 and you'd get an error. See also: Are variables declared with let or const hoisted? Commented Aug 18, 2021 at 7: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". developer.mozilla.org/en-US/docs/Glossary/Hoisting Commented Aug 18, 2021 at 7: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. Commented Aug 18, 2021 at 7: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. Commented Aug 18, 2021 at 7:14

3 Answers 3

1

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

let x;
console.log(x);

>> undefined
Sign up to request clarification or add additional context in comments.

2 Comments

The question is if not declared, to what it is initialized
@Sirode You will see an error : Uncaught ReferenceError: x is not defined fiddle
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

Comments

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)

1 Comment

Undeclared variables are different from var variables. They have global scope, unlike var variables. Refer: avikalpgupta.medium.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.