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

183
Vistas
Why '1' + '2' - 3 gives 9?

I am new to javascript, and I tried to run the below in Brave browser:

'1' + '2' - 3;

The browser replied with the value 9, which I don't understand.

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

0

'1' + '2' is concatenation of strings and result is '12'. '12' - 3 is math operation 12 - 3 and result is 9.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You shoud do the intermediate steps and see for yourself.

Try:

a = '1' + '2';
console.log(a);
b = a - 3;
console.log(b);   // prints 9

The + operator is described as for purpose of sum of numeric operands or string concatenation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition

about 4 years ago · Juan Pablo Isaza Denunciar

0

That is because your first two numbers are strings. you would get a correct answer if you used:

1 + 2 - 3

Or if you cannot avoid doing a calculation on a string, convert it to a number first:

parseInt('1') + parseInt('2') - 3
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