# JS Tip of the Day: Yield Many With yield\*

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-yield-many-with-yield/643117>\
**Category:** web dev\
**Created:** [April 19, 2020, 2:39pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-yield-many-with-yield/643117 "2020-04-19T14:39:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 19, 2020, 2:39pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-yield-many-with-yield/643117/1 "2020-04-19T14:39:11Z")

</div>

**Yield Many With yield\***  
Version: ES2015  
Level: Intermediate

When you yield a value from a generator with `yield` it provides that value as an iteration value when the generator object iterator is used in iteration. When yielding multiple values, you might consider handling this through a loop.

```javascript
function * gen () {
    let nums = [1, 2, 3];
    for (let num of nums) {
        yield num;
    }
}

for (let value of gen()) {
    console.log(value);
}
/* logs:
1
2
3
*/

```

If you’re looping through an iterable this way, you can automatically yield every value in that iterator using `yield*`. `yield*` is a shortcut that will automatically take the values of an iterable and yield them out individually from the current generator function.

```javascript
function * gen () {
    let nums = [1, 2, 3];
    yield * nums;
}

for (let value of gen()) {
    console.log(value);
}
/* logs:
1
2
3
*/

```

Note that if you were try to yield an iterable without the `*`, you’d get the entire iterable in the iteration rather than its individual values.

```javascript
function * gen () {
    let nums = [1, 2, 3];
    yield nums;
}

for (let value of gen()) {
    console.log(value);
}
/* logs:
[1, 2, 3]
*/

```

The use of `yield*` instead lets you spread out the iterable between multiple iterations, not unlike using the spread operator (`...`) in arrays or argument lists.

More info:

- [yield\* on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*)
