-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13.js
More file actions
65 lines (51 loc) · 1.93 KB
/
Day13.js
File metadata and controls
65 lines (51 loc) · 1.93 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// EP-13 | FIRST CLASS FUNCTIONS ft. Anonymous Functions
// What is an Anonymous function?
// What are First class functions?
// Function statement vs Function Expression vs Function Declaration
// Difference between statement and expression: Hoisting
// a(); // will give output "statement"
// b(); // will give TypeError: b is not a function, it is a variable
// Function statement or declaration
function a() {
console.log("statement");
}
a();
// Function expression
// Note: Function acts like a value in JS.
var b = function () {
console.log("expression");
};
b();
// Note:
// When the JS execute code line by line then it will treat 'b' like a variable and assign undefined to
// it in the memory. And when we try to call it before assigning the function it will give the error i.e
// TypeError because JS is treating it like a variable.
// Anonymous function
// function () {} => syntax error need name
const anonymous = function () {};
// These type of functions are used in a place where functions are used as values.
// Named function expression
var by = function xyz() {
console.log("expression");
};
by();
xyz(); // ReferenceError: xyz is not defined
// Difference between parameters and arguments
const q = function (parameters) {
console.log(parameters);
};
q("Arguments");
// First class functions
// As we know that functions are treated as values in JS, and we can also replicate almost everything,
// which we can do with a variable that's why we call functions as First class functions in JS.
// We can assign functions to variables, we can return a function from inside a function, we can pass
// a function as an argument in a function.
// Definition: The ability to use functions as values in JS is make them first class functions or
// we can also call them first class citizens.
function first() {}
function second(first) {
return first;
}
// Arrow functions
// Same can be done with arrow functions
const arrow = () => {};