Categories
JavaScript Answers

How to increment value each time when you run function with JavaScript?

Spread the love

Sometimes, we want to increment value each time when you run function with JavaScript.

In this article, we’ll look at how to increment value each time when you run function with JavaScript.

How to increment value each time when you run function with JavaScript?

To increment value each time when you run function with JavaScript, we can create a variable outside the function and increment that when the function is run.

For instance, we write:

let count = 0
const f = () => {
  count++
}
f()
f()
console.log(count)

to define the count variable.

Then we define function f that increments count by 1.

Then we call f twice.

As a result, count is 2 according to the console log.

Conclusion

To increment value each time when you run function with JavaScript, we can create a variable outside the function and increment that when the function is run.

By John Au-Yeung

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

One reply on “How to increment value each time when you run function with JavaScript?”

Leave a Reply

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