Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda