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.