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

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

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 Report

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 Report

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