Saturday, October 18, 2025
HomeLanguagesJavascriptJavaScript yield* Expression

JavaScript yield* Expression

The yield* expression in JavaScript is used when one wants to delegate some other iterable object. This function iterates over the particular operand and yields each value that is returned by it.

Syntax:

yield* expression;

Return Value: It returns the iterable object.

Example 1: In this example, we will see the basic use of the Javascript yield* expression.

Javascript




<script>
    function* func1() {
      yield "a";
      yield "b";
      yield* func3();
    }
    function* func3() {
      yield "neveropen";
    }
    function* func2() {
      yield* func1();
      yield 4/2;
      yield 5/2;
    }
    const it = func2();
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
</script>


Output:

{value: 'a', done: false}
{value: 'b', done: false}
{value: 'neveropen', done: false}
{value: 2, done: false}
{value: 2.5, done: false}
{value: undefined, done: true}

Example 2: Using other iterable objects with yield* in JavaScript.

Javascript




<script>
    function* func1() {
      yield* func3(1, 2, 3, 4);
    }
    function* func3(){
      yield * Array.from(arguments);
    }
    function* func2(){  
      yield* func1();
      yield* ["from array", "from array"];
    }
    const it = func2();
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
    console.log(it.next()); 
</script>


Output:

{value: 1, done: false}
{value: 2, done: false}
{value: 3, done: false}
{value: 4, done: false}
{value: 'from array', done: false}
{value: 'from array', done: false}

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

Supported Browsers: 

  • Chrome 39 and above
  • Edge 12 and above
  • Firefox 27 and above
  • Opera 26 and above
  • Safari 10 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS