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

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

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