- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is "function*" in JavaScript?
The function* declaration is used to define a generator function. It returns a Generator object. Generator Functions allows execution of code in between when a function is exited and resumed later. So, generators can be used to manage flow control in a code.
Syntax
Here’s the syntax −
function *myFunction() {} // or function* myFunction() {} // or function*myFunction() {}
Let’s see how to use generator function
Example
<html> <body> <script> function* display() { var num = 1; while (num < 5) yield num++; } var myGenerator = display(); document.write(myGenerator.next().value); document.write("<br>"+myGenerator.next().value); document.write("<br>"+myGenerator.next().value); document.write("<br>"+myGenerator.next().value); document.write("<br>"+myGenerator.next().value); </script> </body> </html>
- Related Articles
- What is function overloading in JavaScript?
- What is function chaining in JavaScript?
- What is function() constructor in JavaScript?
- What is super() function in JavaScript?
- What is a function literal in JavaScript?
- What is an anonymous function in JavaScript?
- What is the (function() { } )() construct in JavaScript?
- What is an Arrow Function in JavaScript?
- What is the currying function in JavaScript?
- What is a fat arrow function in JavaScript?
- What is the role of clearTimeout() function in JavaScript?
- What is the use of apply() function in JavaScript?
- What is the use of Math.hypot() function in JavaScript?
- What is the importance of _.initial() function in JavaScript?
- What is the use of Math.imul( ) Function in JavaScript?

Advertisements