-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_function_expression.js
More file actions
23 lines (17 loc) · 1.33 KB
/
Copy path05_function_expression.js
File metadata and controls
23 lines (17 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Topic: Function Expression
// Q. What is a function expression?
// -> A function expression is a way to define a function by assigning it to a variable. Instead of declaring a function using the function keyword followed by a name at the beginning of a statement (which is a function declaration), you create an anonymous (or sometimes named) function as part of an expression, usually an assignment.
// Q. Why use function expression?
// -> Enable Closures and IIFEs: They are necessary for creating IIFEs (Immediately Invoked Function Expressions) and defining functions that close over a specific scope.
// -> Support Callback Functions: They are the standard way to define functions passed as arguments to other functions (e.g., in Array.prototype.map(), setTimeout(), or when handling events).
// -> Control Hoisting: Unlike declarations, expressions are not hoisted completely to the top of their scope, which helps prevent referencing a function before it has been defined, leading to more predictable code flow.
// Example:
// Storing regular named function inside a variable.
function functionName() {
return "I am regular named function stored inside a variable";
};
variable();
// Storing regular unnamed function inside a variable.
const variable1 = function () {
return "I am regular unnamed function stored inside a variable";
};