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