Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is yield classed as an operator and not a statement?

Tags:

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?

like image 474
Ben Avatar asked Apr 25 '20 16:04

Ben


People also ask

Which operator is used to check both value and type in JavaScript?

JavaScript '===' operator: Also known as strict equality operator, it compares both the value and the type that is why the name “strict equality”. Example 1: Let's see some code where '===' operator will return true.

What does {} mean in JavaScript?

So, what is the meaning of {} in JavaScript? In JavaScript, we use {} braces for creating an empty object. You can think of this as the basis for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array.

How do you do or in JavaScript?

The logical OR ( || ) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value.


1 Answers

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);
like image 182
Nina Scholz Avatar answered Sep 30 '22 17:09

Nina Scholz