JavaScript Function Expression:

In JavaScript, a function expression is a way to define a function inside another expression. It’s like creating a function on the fly without giving it a name. Unlike function declarations, function expressions don’t need a name. They can be immediately invoked, meaning they run as soon as they’re defined. To use a function expression, you store it in a variable and then call it using that variable name. With ES6’s arrow function feature, declaring function expressions becomes even simpler.

Syntax for Function Declaration:

function functionName(x, y) { statements... return (z) };

Syntax for Function Expression (anonymous):

let variableName = function(x, y) { statements... return (z) };

Syntax for Function Expression (named): 

let variableName = function functionName(x, y) 
{ statements... return (z) };

Syntax for Arrow Function:

let variableName = (x, y) => { statements... return (z) }; 

Example 2: Code for Function Expression (anonymous

let calSub = function (x, y) {
	let z = x - y;
	return z;
}

console.log("Subtraction : " + calSub(7, 4));

Output: 

Subtraction : 3

Example 3: Code for Function Expression (named

let calMul = function Mul(x, y) {
	let z = x * y;
	return z;
}

console.log("Multiplication : " + calMul(7, 4));

Output:

Multiplication : 28

Example 4: Code for Arrow Function 

let calDiv = (x, y) => {
	let z = x / y;
	return z;
}

console.log("Division : " + calDiv(24, 4));

Output: 

Division : 6

JavaScript function* expression:

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:

// Illustration of function* expression
// use of function* keyword
function* func() {
	yield 1;
	yield 2;
	yield 3;
	yield " - Geeks";
}

let obj = '';

// Function calling
for (const i of func()) {
	obj = obj + i;
}

// Output
console.log(obj);

output:

123 - Geeks

Example 2: Below examples illustrate the function* expression in JavaScript:

// Illustration of function* expression
// use of function* keyword
function* func2(y) {
	yield y * y;
};

function* func1() {
	for (let i = 1; i < 6; i++) {
		yield* func2(i);
	}
};

// Function calling
for (const x of func1()) {

	// Output
	console.log(x);
};

Output:

1
4
9
16
25