How to increment a numeric string by +1 with JavaScript?

Sometimes, we want to increment a numeric string by +1 with JavaScript.

In this article, we’ll look at how to increment a numeric string by +1 with JavaScript.

How to increment a numeric string by +1 with JavaScript?

To increment a numeric string by +1 with JavaScript, we can convert it to a number before incrementing it.

For instance, we write:

let pageID = '1'
pageID = parseInt(pageID) + 1;
console.log(pageID)

to call parseInt with pageID to convert the number string into a number.

Then we add 1 to it and assign the returned value back to pageID.

Therefore, pageID is 2 according to the console log.

Conclusion

To increment a numeric string by +1 with JavaScript, we can convert it to a number before incrementing it.