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

107
Visualizações
Returned value not expected - closures

I don't understand why it returns 0, it is supposed to increment by 1, is there something I don't know about closures?

 let i = 0;
    const getRenderValue = function() {

      return i++;
    };
    const Component = function() {
      let hello = getRenderValue();
      console.log(hello); // return 0, expected value 1
    };
    Component();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your closure does increment the value of i by one. If you did console.log(i); instead of console.log(hello);, you'd be seeing 1 on screen as expected.

Your closure however returns the value of i prior to the increment as it returns i++: It is a property of the postfix increment i++ that it returns i before and not after the increment. Use the prefix increment ++i if you want to return the changed value.

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