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

321
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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