Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *