Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda