• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

232
Views
Javascript var/let/const variable initialization

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?

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

let x;
console.log(x);

>> undefined
almost 3 years ago · Juan Pablo Isaza Report

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)

almost 3 years ago · Juan Pablo Isaza Report

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

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error