5

I was looking at the mdn javascript reference and noticed that yield is listed in the operators section. On the other hand return is listed as a statement. I also found yield has a operator precedence of 2.

What features of yield make it fall into the operator class rather than a statement? Why does return fall into statements rather than operators?

user2864740
62.5k15 gold badges159 silver badges234 bronze badges
asked Apr 25, 2020 at 16:01
1
  • 1
    i think, defination is not clear. await is listed as expression. But it look/work same as yield. Commented Apr 25, 2020 at 16:30

2 Answers 2

3

It is an operator because it can be used in an expression.

function* g() {
 value = 3;
 while (value !== 5) value = Math.floor(yield value + 1);
}
var gen = g();
console.log(gen.next().value);
console.log(gen.next(1.5).value);

answered Apr 25, 2020 at 16:15
Sign up to request clarification or add additional context in comments.

1 Comment

Thats a good perspective. Consider a function statement - it could not be used in that context, while a function expression could.
1

I am not certain on this, but in the context of a generator yield sends data to the generator.next() in that way it operates much like a function. Operators are special classes of functions in the most languages (JavaScript inlcuded).

You could almost imagine the generator.next calling into its instance passing a callback as to where to resume. And yield invoking that callback

Return signals the end of a path of execution and to replace a return value into the proper memory location and unwind the call stack by 1 unit. If feels primordial to the definition of the language,

answered Apr 25, 2020 at 16:09

5 Comments

At the syntax level, does yield act differently? Showing, eg. that "yield a, yield b;", "q = yield a;" or similar would be sufficient to show differences. The Grammar Section will be relevant, as the grammar changes the classification.
I don't know. Both the original question and the one you pose are interesting. What I do know is that operators are all functions of some sort (unary or binary) and work within the are of user defined code. Return has mechanics that seeningly start below that (e.g. the call stack) and extend into the user defined bits.
ecma-international.org/ecma-262/10.0/index.html contains the answers. Return ending the function is not relevant. In C#, "yield" is a statement. In JavaScript, "function" (can be a) is statement that does not affect flow.
The point of discussing return was to contrast the difference between operators and statements in a generalized sense. but I think Nina has it.
"statement" vs. "expression" only exists as a grammatical construct.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.