So i'd like to make a function that sequentially generates a number each execution, I was wondering how to do this in Javascript I need this to basically set an order to my items in mongoDB. How can I write a JS function that generates a number sequentially each time it's run? ex: 1... then run the function again...2 then again...3.
asked Jul 18, 2015 at 22:32
user3649245
531 gold badge1 silver badge5 bronze badges
1 Answer 1
var next = (function() {
var v = 0;
return function() {
return v++;
}})();
console.log(next()); // 0
console.log(next()); // 1
console.log(next()); // 2
answered Jul 18, 2015 at 22:35
Kuba Wyrostek
6,2311 gold badge24 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js
yieldis still (too) rare in JavaScript implementations.