Last updated: Mar 2, 2024
Reading time·2 min

The "SyntaxError: Unexpected strict mode reserved word yield" occurs when the
yield keyword is used outside of a generator function.
To solve the error, mark the function as a generator by appending an asterisk
to the function keyword, e.g. function* generator() {}.

Here's an example of how the error occurs.
function example() { // ⛔️ SyntaxError: Unexpected strict mode reserved word 'yield' yield 10; yield 15; }

We used the yield keyword inside of a function that we didn't mark as a
generator.
Add an asterisk after the function keyword to mark a function as a generator.
// ✅ works function* generator() { yield 10; yield 15; } const gen = generator(); console.log(gen.next().value); // 👉️ 10 console.log(gen.next().value); // 👉️ 15

yield keyword inside of named functions that were declared as generators.The directly enclosing function has to be a generator for you to be able to use
the yield keyword.
Here's an example of nested functions.
function generator() { return function* inner() { yield 10; yield 15; }; } const gen = generator()(); console.log(gen.next().value); // 👉️ 10 console.log(gen.next().value); // 👉️ 15

Notice that we marked the inner function as a generator and not the outer one.
This is because we use the yield keyword inside of the inner function.
To be able to use the yield keyword, declare the directly enclosing function
as a generator.