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

163
Visualizações
Why is let variable accessible within IIFE & var is not?

This IIFE code is able to access count

let count = 0;
(function immediate() {
  if (count === 0) {
    let count = 1;
    console.log(count);
  }
  console.log(count);
})();

But why is count undefined in this case?

var count = 0;
(function immediate() {
  if (count === 0) {
    var count = 1;
    console.log(count);
  }
  console.log(count);
})();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you use var the variable declaration is hoisted, that mean that your code will be rewritted in the following way:

var count = 0;
(function immediate() {
  var count; // decalration of variable hoisted on the function scope
             // and the count variable now is undefined.
  if (count === 0) {
    count = 1;
    console.log(count);
  }
  console.log(count); // just this is printed.
})();

As you can se, the variable inside the if block is now overriding the global count.

The fact that this behaviour is not so obvius is the reason why many suggest to avoid using var as we can use let and const instead, that have a block scope.

Update

Thanks to the comment of @t.niese I think is wort to mention another difference between var vs let and const.

Like var, let and const are hoisted, but with this differencese:

  1. they have a block scope and not a function scope like var has
  2. they are declared but not initialized to undefined like var
  3. any attempt to access a variable declared with let and const before it is initialized will result in a ReferenceError exception, while accessing an undeclared variable or a hoisted var will return just undefined.
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