since javascript will bound variable scope to its declaration,
or says it will keep the local context.
let’s define a function that will return a function for step add.

1
2
3
4
5
6
7
function step_iterator(start, step) {
return function() {
var res = start;
start = start + step;
return res;
}
}

then we can get an iterator by call the function

1
var numberWithStep2 = step_iterator(0, 2);

and javascript will keep it as a function in numberWithStep2

each time we call the function numberWithStep2

1
2
3
4
5
6
numberWithStep2();
// will return 0
numberWithStep2();
// will return 2
numberWithStep2();
// will return 4

pretty cool! huh?