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

261
Visualizações
Simple counter with setInterval() not working?

I would like to make a simple counter with setInterval(), like this:

setInterval(() => {
  if(typeof i === 'undefined') {
    let i = 1;
  } else {
    i++;
  }
  console.log(i);
}, 1000);

What this does is it crashes saying how i is not defined and I would like to understand why?

Does it fail cause console.log(i) is not in the same scope as where let i = 1 is? So in JavaScript every { } block has its own scope? I know I could define i = 1 without if else, but would like to learn why does this example fail to work? Is it cause of scope?

Also, I would like this to work with defining a variable, only if it does not exist, something like isset() in PHP!

about 4 years ago · Juan Pablo Isaza
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