To solve this problem, we'll use generator in Javascript. A generator is a function that returns (yield) sequence of results instead of a single result. We define a generator function in JS as: function * fibonacci ( ) { // to be implemented } We implement the function above as: function * fibonacci ( ) { let a = 0 , b = 1 ; while ( true ) { yield a a = b, b =a +b; } } You can test it like this: const result = fibonacci() console .log(result.next().value); //0 console .log(result.next().value); // 1 console .log(result.next().value); // 2 console .log(result.next().value); // 4 console .log(result.next().value); // 8 console .log(result.next().value); //16 Each time you call result.next().value , a new fibonacci number will be returned