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

145
Views
adding and subtracting a string-number works differently in javascript

HTML

<body>
    <h1>Counter</h1>
    <p id="num">0</p>
    <div>
        <button id="minus">-</button>
        <button id="plus">+</button>
    </div>
    <script src="src/main.js"></script>
</body>

JS

const num = document.getElementById('num')
const minus = document.getElementById('minus')
const plus = document.getElementById('plus')

minus.addEventListener('click', () => {
  num.textContent -= 1;
})

plus.addEventListener('click', () => {
  num.textContent += 1;
})

This is a simple counter app. If I press minus btn, it acts as expected (i.e. subtracts 1 from number) But if I press plus, I get a string concatenation (i.e. '01')

I'm confused here. Why one acts like a number and the other string? it seems like there is a auto-conversion of type in very inconsistant way.

and what would be the best practice to fix 'plus'?

about 4 years ago · Juan Pablo Isaza
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!