Edward Jackson

Edward Jackson


The Basics of JavaScript Generators

What Are JavaScript Generators?

JavaScript generators are just ways to make iterators. They use the yield keyword to yield execution control back to the calling function and can then resume execution once the next() function is called again. Once the generator runs out of values to return, it returns a value of undefined, and a done value of true, letting you know that there are no more values. This makes more sense if you can see a simple example.

function* foo() {
  yield 'a';
  yield 'b';
  yield 'c';

const foogee = foo();

console.log(‘getting the first value’);
console.log(‘getting the next value’);
console.log(‘getting the next value’);
console.log(‘getting the next value’);

The function foo() is a generator function, indicated by the * after the function declaration. The constant foogee instantiates the foo generator function, but simply returns a type of iterator called a Generator. The next() function always returns an object with a value and a done property. The very first time next() is called on the Generator, the function executes the first line which yields execution control back to the caller with a return value of:

{ value: ‘a’, done: false }

The program logs it to the console. The program then calls next() again, which resumes execution of the generator, yielding a value of:

{ value: ‘b’, done: false }

The third time, it yields the return object with a value of:

{ value: ‘c’, done: false }

Now the generator is out of values to yield, so the next time the program calls next(), it returns:

{ value: undefined, done: true }

From this point on, no matter how many more times the program calls next() it will always return that object.

What Would I Use JavaScript Generators For?

You might think, “That’s cool, but I can already iterate over collections in JavaScript. Why would I need to create a JavaScript Generator?”

That’s a fair question. What about things that don’t automatically come in a collection? Like the all-powerful Fibonacci Sequence?

function* fibonacci() {
let current = 0;
let next = 1;

yield current;
[current, next] = [next, current + next];

const gen = fibonacci();
for(let i = 0; i < 10; i++){

This fibonacci generator simply yields the next value in the Fibonacci sequence. There isn’t really a Fibonacci array in JavaScript to iterate over so you need to make your own.

A More Practical Use for JavaScript Generators

While it’s totally cool to nerd out on creating fibonacci generator functions, it’s not super useful for all of you out there writing business software, right? So how about dates? There’s no real way to iterate over all the days in a month or the next thirty days. Create a JavaScript Generator for that!

function* dateGenerator(startDate = new Date()) {
let currentDate = startDate;
while (true) {
yield currentDate;
currentDate.setDate(currentDate.getDate() + 1);

console.log(‘Next 30 Days’);
const dates = dateGenerator();
for(let i = 0; i < 30; i++){

This kind of generator can be super useful in everyday business software. Think about creating calendars in a React, Angular, or Vue app. What about saving a reminder to a todo list every day for the next 30 days?

Normally, you’d have to create an array of these values by looping over the line that sets the date.

currentDate.setDate(currentDate.getDate() + 1);

Once you had the size of array you want, it would loop over that array to get the value you want. This is particularly useful for those kinds of things that don’t readily have a collection that you can iterate over.

What uses can you think of for generators?

Learn More JavaScript Goodness!

Introducing TensorFlow.js: Machine Learning in Javascript

Full Stack Developers: Everything You Need to Know

ES5 to ESNext — here’s every feature added to JavaScript since 2015

5 Javascript (ES6+) features that you should be using in 2019

The Complete JavaScript Course 2019: Build Real Projects!

JavaScript: Understanding the Weird Parts

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

The Full JavaScript & ES6 Tutorial - (including ES7 & React)

Originally published by Lee Brandt at https://developer.okta.com


What is GEEK

Buddha Community

The Basics of JavaScript Generators

eungyu Lee


Nice to meet you, Jackson. I’m Eungyu Lee, the Front-end developer of Korea Your posting is very attractive. I’d like to translate this, if you don’t mind, would you let me?

anita maity

anita maity


Random Password Generator Using JavaScript, HTML & CSS

Random Password Generator is a program that automatically generates a password randomly. Those generated passwords are mix with numbers, alphabets, symbols, and punctuations. This type of program helps the user to create a strong password.

Step By Step Tutorial :https://cutt.ly/ZbiDeyL

#password generator #random password generator #python password generator #random password generator javascript #html #javascript

Introduction With Basic JavaScript

The world’s most misunderstood programming language is JavaScript but JavaScript is now used by an incredible number of high-profile applications. So, it’s an important skill for any web or mobile developer to enrich the deeper knowledge in it.

Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world.

Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes. JavaScript also supports functional programming — because they are objects, functions may be stored in variables and passed around like any other object.

Let’s start off by looking at the building blocks of any language: the types. JavaScript programs manipulate values, and those values all belong to a type. JavaScript’s types are:

· Number

· String

· Boolean

· Function

· Object

· Symbol

and undefined and null, which are … slightly odd. And Array, which is a special kind of object. Date and RegExp, which are objects that you get for free. And to be technically accurate, functions are just a special type of object. So the type of diagram looks like this:

#beginner-javascript #javascript #javascript-introduction #javascript-fundamental #basic-javascritp

Vincent Lab

Vincent Lab


JavaScript Password Generator

In this video, I will be showing you how to build a password generator in JavaScript.

#password generator #random password generator #password #javascript #javascript project #javascript fun project

Vincent Lab

Vincent Lab


JavaScript Password Generator - Part 2

In this video, I will be showing you how to build a password generator in JavaScript

#password generator #random password generator #javascript #js #javascript fun project #javascript project

Rahul Jangid


What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */


Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript