{"id":423,"date":"2023-07-23T02:16:42","date_gmt":"2023-07-23T02:16:42","guid":{"rendered":"https:\/\/nikspace.co.in\/?p=423"},"modified":"2023-07-23T02:18:59","modified_gmt":"2023-07-23T02:18:59","slug":"javascript-generator","status":"publish","type":"post","link":"https:\/\/nikspace.co.in\/?p=423","title":{"rendered":"JavaScript generator"},"content":{"rendered":"\n<p>The feature I will be discussing in this post is generators. This is a new feature that was introduced in ES6.<br>A generator is a function that can be stopped midway and start off from where it left. This is very different from a normal function which runs to completion and if started again will start from the beginning.<br>Let\u2019s look at a normal function for generating Fibonacci numbers for n terms:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      \/\/ program to generate fibonacci series up to n terms\n      \/\/ take input from the user\n      const number = parseInt(prompt(\"Enter the number of terms: \"));\n\n      console.log(\"Fibonacci Series:\");\n      for (i of fibonacci(number))\n      {\n        console.log(i);\n      }\n\n      function fibonacci(nums)\n      {\n        let fib = &#91;0, 1];\n        let data = &#91;];\n\n        for(let i = 2; i &lt;= nums; i++)\n        {\n          fib&#91;i]=fib&#91;i - 1] + fib&#91;i - 2];\n          data.push(fib&#91;i]);\n        }\n        return data;\n      }<\/code><\/pre>\n\n\n\n<p>Code pen link &#8211; <a href=\"https:\/\/codepen.io\/ragesh_d\/pen\/yLzNQPG\" target=\"_blank\" rel=\"noopener\">https:\/\/codepen.io\/ragesh_d\/pen\/yLzNQPG<\/a><\/p>\n\n\n\n<p>This is how you would create one using generators:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/ Fibonacci generator\n      function* fibonacci()\n      {\n        &#91;a, b] = &#91;0, 1]\n        while (true)\n        {\n          yield a;\n          &#91;a, b] = &#91;b, a + b]\n        }\n      }\n      \/\/ Instantiates the fibonacci generator\n      var fib = fibonacci();\n      const number = parseInt(prompt('Enter the number of terms: '));\n      \/\/ gets first n numbers from the Fibonacci generator starting from 0\n      for (let i = 0; i &lt; number; i++)\n      {\n        console.log(fib.next().value);\n      }<\/code><\/pre>\n\n\n\n<p>Code pen link &#8211; <a href=\"https:\/\/codepen.io\/ragesh_d\/pen\/dyVoQdp\" target=\"_blank\" rel=\"noopener\">https:\/\/codepen.io\/ragesh_d\/pen\/dyVoQdp<\/a><\/p>\n\n\n\n<p>The advantage of the generator method is that the calling program does not get back a list but just the next value, this means that it does not need to store the whole list so space wise it\u2019s a much more efficient approach.<br>Now let\u2019s see the syntax to creating a generator function, as can be seen from the example the function* is used to differentiate a generator from a normal function. It also uses a yield which is like a return but the function will halt after returning until the calling function invokes it using a next().<br>Let\u2019s go over some advantages of generators:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Generator values are lazily evaluated and until a value is request it does not get computed<\/li><li>Using generator can let you do much more memory efficient operations<\/li><\/ul>\n\n\n\n<p>Some disadvantages of generators are:<br>1. Once a generator has looped over a possible set of values it can\u2019t restart , that\u2019s why it\u2019s usually to keep generators in a infinite loop.<br>2. It\u2019s not easily possible to go back in value with a generator<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1. Once a generator has looped over a possible set of values it can\u2019t restart , that\u2019s why it\u2019s usually to keep generators in a infinite loop.<\/li><li>2. It\u2019s not easily possible to go back in value with a generator<\/li><\/ul>\n\n\n\n<p>References:<br><a href=\"https:\/\/github.com\/lukehoban\/es6features#promises\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/lukehoban\/es6features#promises<\/a><br><a href=\"https:\/\/codeburst.io\/understanding-generators-in-es6-javascript-with-examples-6728834016d5\" target=\"_blank\" rel=\"noopener\">https:\/\/codeburst.io\/understanding-generators-in-es6-javascript-with-examples-6728834016d5<\/a><\/p>\n\n\n\n<p>By Ragesh<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The feature I will be discussing in this post is generators. This is a new feature that was introduced in ES6.A generator is a function that can be stopped midway and start off from where it left. This is very different from a normal function which runs to completion and if started again will start [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-423","post","type-post","status-publish","format-standard","hentry","category-software"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nikspace.co.in\/index.php?rest_route=\/wp\/v2\/posts\/423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikspace.co.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikspace.co.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikspace.co.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikspace.co.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=423"}],"version-history":[{"count":2,"href":"https:\/\/nikspace.co.in\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/nikspace.co.in\/index.php?rest_route=\/wp\/v2\/posts\/423\/revisions\/425"}],"wp:attachment":[{"href":"https:\/\/nikspace.co.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikspace.co.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikspace.co.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}