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

327
Vistas
Why it only add the number with += and not +

I am learning to code and trying out some JavaScript programming and came across this which I don't understand. Please help! I don't understand why it wouldn't add the number with '+' but does with '+='.

let number = 10;
number + 10; //Return 10
number += 10; //Return 20
console.log(number);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

There is an important distinction to make between and expression and an instruction. An expression evaluates to a value, but doesn't necessarily have an effect. An instruction doesn't necessarily evaluate to a value, but has some effect.

In your example, number + 10 is an expression that evaluates to 20. But it has no effect by itself.

By contrast, number += 10 is an instruction that modifies the value of variable number.

Note that number += 10 is essentially equivalent to number = number + 10.

The last line in your code, console.log(number), is also an instruction: it prints the value of number.

Have you tried the instruction console.log(number + 10)?

about 4 years ago · Juan Pablo Isaza Denunciar

0

'+=' operator come from C language and it's a shortcut for

number = number + 10

so in your code all works, but number + 10 is just operation without assignment;

about 4 years ago · Juan Pablo Isaza Denunciar

0

The line number + 10 will calculate the value but you need to assign this value to the variable (to store it in the variable).

Indeed, you need to write number = number + 10.

About the syntax += : It only makes the code easier to read, the two following make exactly the same thing :

  • number = number + 10
  • number += 10

For more informations Addition assignment

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