Skip to main content

Command Palette

Search for a command to run...

JavaScript: Generator function example

Published
•1 min read•View as Markdown
JavaScript: Generator function example
H

Full stack developer (ReactJS, NodeJS, JavaScript, PHP, SQL)

function* gen()  
{  
  yield 100;  
  let a = 100
  a = a + 1
  yield a;
  a = 200
  yield a * 2;
}

// Calling the Generator Function  
var mygen = gen();  
console.log(mygen.next().value);
console.log(mygen.next().value);
console.log(mygen.next().value);
Output:
100
101
400

Execute above code here

More from this blog

E

Everyday Dev Patterns

26 posts

A collection of real-world JavaScript snippets, patterns, and gotchas from my day-to-day work.