Javascript add +1 to string

I have a variable that stores innerHTML text in it

var text = document.getElementById("textID").innerHTML; // <-- textID is actually a number

"text" is actually just a number, but I think javascript still considers it a string.

I want to add +1 to the variable text, but it just adds a new letter instead of increasing the number

For example: 0 + 1 = 01 → 01 + 1 = 011, etc.

Here is the code I tried with:

text = text + 1;

How can I do this so that it increases the number instead of adding new letters? (1 + 1 = 2 etc.)

+4
source share
1 answer

Under the assumption that you are absolutely sure that the number is a decimal integer. Or

text = +text + 1;

Or

text = parseInt(text, 10) + 1;

or

text = Number(text) + 1;
+8
source

Source: https://habr.com/ru/post/1614298/


All Articles