Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
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?

about 4 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
about 4 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)

about 4 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

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!