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

106
Visualizações
javascript function output problrm

The output of console.log(in1()) should be 11 but it's 10 why?

function outer() {
  let i = 10;
  return function inner() {
    return i++;
  }
}

var in1 = outer();
console.log(in1());

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Quoting https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment here:

If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.

Therefore, the postfix (that is, x++; postfix because it appears after) operator only gives the existing value before doing anything. That's why, when called, gives what exists, not what's the result of the addition.

Because of this, use the prefix operator instead, ++x, to get the value while still incrementing the variable.

The same applies to the decrement operator.

about 4 years ago · Juan Pablo Isaza Relatório

0

It's because using i++ returns i first, then adds 1 to i. If you call in1() the second time, you will see 11, 12 and so on. Try using ++i instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

When using the postfix syntax i++ the value is returned before incrementing. Use the prefix syntax instead ++i.

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