Organizational Research By

Surprising Reserch Topic

what is function in javascript

what is function in javascript  using -'javascript,function,ecmascript-6'

In this page I found a new JavaScript function type:

// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13

function* fibonacci() { // !!! this is the interesting line !!!
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;

I already know what yield, let and [?,?]=[?,?] do, but have no idea what the function* is meant to be. What is it?

P.S. don't bother trying Google, it's impossible to search for expressions with asterisks (they're used as placeholders).

asked Sep 14, 2015 by Sherri3192u
0 votes

Related Hot Questions

3 Answers

0 votes

It's a proposed syntax for

Dave Herman of Mozilla gave a talk about At 30:15 he talks about generators. Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.

You can find more detail on the EcmaScript working group wiki:

The working group (TC-39) has general agreement that should have some kind of generator iterator proposal, but this is not final. You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.


First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.


The ‚Äúinfinite‚ÄĚ sequence of Fibonacci numbers (notwithstanding behavior around 253):

function* fibonacci() {
    let [prev, curr] = [0, 1];
    for (;;) {
        [prev, curr] = [curr, prev + curr];
        yield curr;

Generators can be iterated over in loops:

for (n of fibonacci()) {
    // truncate the sequence at 1000
    if (n > 1000)

Generators are iterators:

let seq = fibonacci();
print(; // 1
print(; // 2
print(; // 3
print(; // 5
print(; // 8
answered Sep 14, 2015 by IlaEddingto
0 votes

It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...

Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.

answered Sep 14, 2015 by EllPendleto
0 votes

The function* type looks like it acts as a generator function for processes that can be iterated. C# has a feature like this using "yield return" see 1 and see 2

Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.

answered Sep 14, 2015 by RMcConnel