Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!