- 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 the usage of yield keyword in JavaScript?
The yield keyword is used in JavaScript to pause and resume a generator function. The value of the expression is returned to the generator's caller.
Here’s the syntax, where “exp” is the expression and the optional value is returned by “val”, which is passed to the generator's next() method.
[val] = yield [exp];
Here are the examples:
function* displayRank () { var selPlayers= [1, 2, 3, 4]; for (var a = 0; a < selPlayers.length; a++) { yield selPlayers[i]; } }
After defining a generator function, use it like the following.
Here displayRank() is the generator function:
var rank = displayRank(); // // value: 1 alert(rank.next()); // value: 2 alert(rank.next()); // value: 3 alert(rank.next()); // value: 4 alert(rank.next()); // value: undefined alert(rank.next());
- Related Articles
- What is the yield keyword in JavaScript?
- The yield* expression/keyword in JavaScript.
- Yield keyword in Ruby Programming
- What is the usage of in operator in JavaScript?
- What is the usage of join() method in JavaScript?
- What is the usage of onhashchange event in JavaScript?
- What is the usage of onpageshow event in JavaScript?
- What is the usage of onpagehide event in JavaScript?
- What is the usage of onscroll event in JavaScript?
- What is the usage of onresize event in JavaScript?
- What is the usage of onunload event in JavaScript?
- What is the usage of onblur event in JavaScript?
- What is the usage of onfocus event in JavaScript?
- What is the usage of onfocusin event in JavaScript?
- What is the usage of onfocusout event in JavaScript?

Advertisements