Tiny Frontend Logo
Posts 0122

On This Page

4. Usage

Iterators and Generators in JavaScript

Let's discover some hidden gems and not so commonly used concepts in JavaScript. Iterators and Generators bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of for...of loops.

Iterators

In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination.

It's an object with the following properties:

  • value: Value held currently by the iterator.

  • done: A boolean flag to determine if the iterator has been consumed.

Generators

Generators are a special type of iterators returned by a Generator function. A generator has the following properties:

  • next(): This method consumes the iterator and returns the next value.

  • return(value: any): This method is used to mark the iterator as consumed and returns the value passed as the argument.

  • throw(error: any): This method is used to throw an error passed as the argument.

Subscribe to read the full content

* Free subscribe to enjoy full content access.
* You can cancel anytime with 1-click.

Read Next

0121

Lazy Load Offscreen Iframes

As more websites embed YouTube videos into their pages to provide richer content, and it indeed helps users access information more effectively.

Lazy Load Offscreen Iframes
0120

Resumable File Upload

The cloud storage has become an indispensable file backup and sharing solution in our daily work. But have you ever considered:

Resumable File Upload
0119

Sending Notifications

The Notification API allows web developers to create and display desktop notifications to users.

Sending Notifications
0118

Higher-Order Functions (HOF) in JavaScript

In JavaScript, functions are treated as first-class citizens. We can use functions as values, assign them to variables, pass them as arguments to other functions, or even return them from other functions.

Higher-Order Functions (HOF) in JavaScript
0117

JavaScript Closures

This question frequently arises during interviews. If you're unsure, here is everything you should know!

JavaScript Closures
0116

Ordering Flexible Elements

In many cases, we rely on the flex-direction property to manage the display order of flexible items.

Ordering Flexible Elements