-
Notifications
You must be signed in to change notification settings - Fork 5
/
09_functions.js
96 lines (87 loc) · 3.53 KB
/
09_functions.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function functions() {
/**
* ========================================================
* Function Declaration
* ========================================================
* In a function declaration, the function is defined using the 'function' keyword and can be called by its name.
* Function declarations are hoisted, making them available throughout their scope.
*/
function sayHello(name) {
return `Hello, ${name}`;
}
console.log(sayHello("Ishtmeet")); // Outputs: "Hello, Ishtmeet"
/**
* ========================================================
* Function Expression
* ========================================================
* A function expression assigns a function to a variable.
* Unlike function declarations, function expressions are not hoisted.
*/
const sayGoodbye = function (name) {
return `Goodbye, ${name}`;
};
console.log(sayGoodbye("Bob")); // Outputs: "Goodbye, Bob"
/**
* ========================================================
* Arrow Function
* ========================================================
* Arrow functions, introduced in ES6, provide a more concise syntax for function expressions.
* Note that 'this' behaves differently in arrow functions.
*/
const add = (a, b) => a + b;
console.log(add(3, 4)); // Outputs: "7"
/**
* ========================================================
* Callback Functions
* ========================================================
* Functions can be passed as arguments to other functions.
* This enables a higher-order programming paradigm.
*/
function process(callback, value) {
return callback(value);
}
console.log(process(add.bind(null, 5), 3)); // Outputs: "8"
/**
* ========================================================
* IIFE (Immediately Invoked Function Expression)
* ========================================================
* An IIFE is a function that runs as soon as it is defined.
* It's a way to run functions immediately, to use variable scope in a specific way.
*/
(function () {
console.log("IIFE executed"); // Outputs: "IIFE executed"
})();
/**
* ========================================================
* Default Parameters
* ========================================================
* Starting with ES6, default parameter values can be defined in the function signature.
*/
function greet(name = "Guest") {
console.log(`Hi ${name}`); // Outputs: "Hi Guest" if no argument is provided
}
greet();
greet("Emily"); // Outputs: "Hi Emily"
/**
* ========================================================
* Rest Parameters
* ========================================================
* Rest parameters (introduced in ES6) allow functions to accept multiple arguments and collect them into an array.
*/
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); // Outputs: "10"
/**
* ========================================================
* Function Hoisting
* ========================================================
* Function declarations are hoisted, allowing them to be used before they are defined in the code.
* Function expressions are not hoisted.
*/
console.log(earlyInvoke()); // Outputs: "Hoisted"
function earlyInvoke() {
return "Hoisted";
}
}
functions();