-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_first_class_function.js
More file actions
43 lines (35 loc) · 1.12 KB
/
Copy path10_first_class_function.js
File metadata and controls
43 lines (35 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Topic: First Class Function
// Q. What is First Class Function?
// -> In JavaScript, functions are considered "first-class" citizens, It means they can be treated just like any other value.
// Functions can be assigned to variables
const variable = function () {
return "I am function assigned to variable";
};
// Functions can be stored in data structures
const variable1 = variable;
variable1();
// Functions can be passed as arguments to other functions
function function1() {
return "I am function1 passed as argument";
}
function function2(function1) {
return function1();
}
// Functions can be returned as values from other functions
function function3() {
return function function4() {
return "I am function4 returned from function3";
};
}
// Functions can be defined inside other functions (nested functions or closures)
function outerFunction() {
function innerFunction() {
return " I am Inner function";
}
innerFunction();
}
outerFunction();
// Functions can be invoked immediately (immediately invoked function expressions)
(function () {
return "I am Immediately Invoked Function Expression";
})();