function* घोषणा का उपयोग जेनरेटर फ़ंक्शन को परिभाषित करने के लिए किया जाता है। यह एक जेनरेटर ऑब्जेक्ट लौटाता है। जेनरेटर फ़ंक्शन फ़ंक्शन के बाहर निकलने और बाद में फिर से शुरू होने पर कोड के बीच में निष्पादन की अनुमति देते हैं। इसलिए, जेनरेटर का उपयोग कोड में फ्लो कंट्रोल प्रबंधित करने के लिए किया जा सकता है।
सिंटैक्स
function *myFunction() {} // या function* myFunction() {} // या function*myFunction() {}
उदाहरण
<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>