JavaScript function* expression
Last Updated :
07 Aug, 2023
The function* is an inbuilt keyword in JavaScript which is used to define a generator function inside an expression.
Syntax:
function* [name]([param1[, param2[, ..., paramN]]]) {
statements
}
Parameters: This function accepts the following parameter as mentioned above and described below:
- name: This parameter is the function name.
- paramN: This parameter is the name of an argument to be passed to the function.
- statements: These parameters comprise the body of the function.
Example 1: Below examples illustrate the function* expression in JavaScript:
Javascript
function * func() {
yield 1;
yield 2;
yield 3;
yield " - Geeks" ;
}
let obj = '' ;
for (const i of func()) {
obj = obj + i;
}
console.log(obj);
|
Example 2: Below examples illustrate the function* expression in JavaScript:
Javascript
function * func2(y) {
yield y * y;
};
function * func1() {
for (let i = 1; i < 6; i++) {
yield* func2(i);
}
};
for (const x of func1()) {
console.log(x);
};
|
Supported Browsers:
The browsers supported by JavaScript function* expression are listed below:
- Google Chrome 49 and above
- Edge 12 and above
- Firefox 26 and above
- Opera 36 and above
- Safari 10 and above