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

193
Vistas
Is there any difference between bag= bag + n - i +1 and bag += n - i + 1

Is there any difference between bag= bag + n - i +1 and bag += n - i + 1 bcs is shows differnt while running a program below:

var n = 5
for (i = 1; i <= n; i++) {
  var bag = ""

  for (j = 1; j <= n - i + 1; j++) {
    bag += n - j + 1
  }
  console.log(bag)
}
// 2nd way
var n = 5; // 
for (let i = 1; i <= n; i++) {
  var string = "";
  for (let j = 1; j <= n - i + 1; j++) {
    string = string + n - j + 1;
  }
  console.log(string);
}

Replit link

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

bag = bag + n - i + 1 and bag += n - i + 1 are not the same.

  • In the former, bag is coerced into a number
  • In the latter, the result is being coered into a string

The coercision occurs at the subtraction operator:

console.log('' + 1);
//=> '1'

console.log('' - 1);
//=> -1

about 4 years ago · Juan Pablo Isaza Denunciar

0

Yes, there is a difference. The difference is to do with the order that your operators are evaluated:

bag += n - i + 1

is the same as:

bag = bag + (n - i + 1) // string concatenation as `bag` is a string and we're using `+`

Notice that n - i + 1 is grouped (( )) because it is on the right-hand side of the +=, so it is evaluated/calculated first before being concatenated with the string bag. Both of the above lines of code are different from your other alternative of:

bag = bag + n - i +1

Here there is no grouping, so the order in which the operators are evaluated is different (see operator precedence). The above is equivalent to:

bag = (bag + n) - (i + 1) // (string) - (number)

... which you can hopefully see will give different results from the first two lines above, as you are now subtracting different quantities. Moreover, as you're using subtraction, your results will now be numbers.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Why?

There is a difference because you are using the "+" operator for concatenation and using a "-" in the same line, so JS is implicitly casting between strings and numbers for you.

In the example of bag += n - j + 1, JS evaluates everything on the right side from left-to-right. Then that number is concatenated onto the end of the string named bag.

In the example of bag = bag + n - j + 1, JS again evaluates everything on the right side from left-to-right, but now we have a mixture of strings and numbers on the right, so you're seeing strange behavior resulting from the anti-pattern.

See here:

bag += n - j + 1       // => bag += 5 - 1 + 1     => bag += 5          => bag = "5"
bag = bag + n - j + 1  // => bag = "" + 5 - 1 + 1 => bag = "5" - 1 + 1 => bag = 5

On the next loop, with the second syntax, you'll be adding to a number instead of concatenating to a string, which is what is causing your problem.

Solution

To fix this, you can technically just keep the first syntax, or you can wrap the numbers in parentheses, like so: bag = bag + (n - j + 1), but I would recommend considering making your casts explicit, like bag += String(n - j + 1) or bag = bag.concat(n - j + 1) and prevent mixing data types to ensure bugs like these don't come up again.

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