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

113
Vistas
How does subtraction operator trigger an array-to-number conversion?

Here are 2 examples of my code. The first one is:

console.log([4] + 10); //"410" 

As far as I know, addition operator can work only with numbers and strings. So, firstly, [4] should be transformed into the number or string. When trying to convert an operand to a primitive data type, either valueOf() or toString() runs. By default, valueOf() returns an array, so this method is ignored. toString() is invoked. It converts the complex data type to a string. That's completely understandable for me, but...

What's going on in the second example?

console.log(10 - [4]); //6 

I don't get how valueOf() can transform array into the number if this method by default returns an array. What does occur here?

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

0

It goes this way:

10 - [4]
10 - '4' // array to string
10 - 4   // implicit casting of all operands to number with minus operator
6        // result
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