1
00:00:01,080 --> 00:00:02,460
In this video.

2
00:00:02,640 --> 00:00:09,840
Let's talk about high order function and function as a first class citizen.

3
00:00:10,200 --> 00:00:17,310
So what is a difference is that is what we are going to talk about in this particular video.

4
00:00:17,820 --> 00:00:23,310
So let's start with function as first class citizen.

5
00:00:23,310 --> 00:00:25,350
What does it mean?

6
00:00:26,240 --> 00:00:29,090
A function is a first class citizen.

7
00:00:29,300 --> 00:00:38,510
If it can be used like any other value in the language, well, it means that if a function can be passed

8
00:00:38,510 --> 00:00:44,430
as an argument to other functions, then it's being regarded as a first class citizen.

9
00:00:44,450 --> 00:00:46,130
That is one point.

10
00:00:47,040 --> 00:00:55,020
And if it can be retained by other function, meaning a function that can be retained by other functions.

11
00:00:55,050 --> 00:00:59,070
In that case, we can regard that one as a first class citizen.

12
00:00:59,850 --> 00:01:07,470
And if we can assign the function to a variable, then it can be regarded as a first class citizen.

13
00:01:08,640 --> 00:01:17,370
And again, if we can store a function inside an array, then it can be regarded as first class citizen.

14
00:01:17,760 --> 00:01:23,070
And in JavaScript, all functions can behave in this way.

15
00:01:23,520 --> 00:01:24,990
So that's what we call.

16
00:01:25,800 --> 00:01:29,130
Functions as first class citizen.

17
00:01:29,400 --> 00:01:33,240
So what about high order functions?

18
00:01:33,270 --> 00:01:42,810
Well, a high order function is a function that takes one or more functions as a segment or retain a

19
00:01:42,810 --> 00:01:43,560
function.

20
00:01:44,190 --> 00:01:51,480
Well, so it means that in JavaScript, all functions are first class citizens.

21
00:01:51,690 --> 00:01:55,980
But not all functions are high order functions.

22
00:01:56,130 --> 00:02:00,870
So let's have a look at example with code.

23
00:02:01,050 --> 00:02:06,390
So when you say functions retaining a function, it can be regarded as what?

24
00:02:06,390 --> 00:02:07,650
First class citizen.

25
00:02:07,890 --> 00:02:08,580
Right.

26
00:02:08,699 --> 00:02:12,380
Or high order function because it takes in another function.

27
00:02:12,390 --> 00:02:13,770
So let's see.

28
00:02:13,800 --> 00:02:14,880
A nice command.

29
00:02:14,880 --> 00:02:20,190
Say function functions returning.

30
00:02:21,040 --> 00:02:21,850
Function.

31
00:02:22,900 --> 00:02:23,740
Is it possible?

32
00:02:23,770 --> 00:02:24,670
Yes, it is.

33
00:02:24,820 --> 00:02:26,110
So let's see.

34
00:02:26,390 --> 00:02:30,400
Example of a function that's going to retain another function.

35
00:02:30,610 --> 00:02:37,030
So let's say I have a function that's going to add two numbers together, two nums.

36
00:02:37,850 --> 00:02:44,810
For the first function or the outer function, it will not take in any agreement, but instead of returning

37
00:02:44,810 --> 00:02:53,240
a value, this function is going to retain a function as that, and this function will pass in A and

38
00:02:53,240 --> 00:02:58,250
B, because I want to add two numbers together and here we go.

39
00:02:58,730 --> 00:03:02,570
So now this is what we call anonymous function.

40
00:03:03,370 --> 00:03:05,230
Because it has no name.

41
00:03:05,320 --> 00:03:10,450
So a function without a name is regarded as an anonymous function.

42
00:03:10,660 --> 00:03:18,270
So now inside my inner function, I can go ahead and retain a plus P as that.

43
00:03:18,280 --> 00:03:22,140
So let's see how can we call this particular function?

44
00:03:22,150 --> 00:03:24,940
So come down here and let's say invoke.

45
00:03:25,420 --> 00:03:29,440
Well, let's deduce it when I call this function call.

46
00:03:29,440 --> 00:03:31,900
Add two numbers as that.

47
00:03:32,920 --> 00:03:36,520
Let's see, what is the response coming back from this?

48
00:03:36,550 --> 00:03:41,590
Is it going to retain a value or a function is going to retain a function.

49
00:03:41,590 --> 00:03:47,030
So let's assign to a variable, say answer.

50
00:03:48,450 --> 00:03:52,260
If I console.log answer what would be the result?

51
00:03:52,740 --> 00:03:54,300
Is it going to be the answer?

52
00:03:54,330 --> 00:03:56,690
Let's say A plus B or a function.

53
00:03:56,700 --> 00:03:57,540
Have a look.

54
00:03:57,750 --> 00:04:02,280
If I console.log, answer now and see that I get a function.

55
00:04:03,270 --> 00:04:04,820
Because it's a function.

56
00:04:04,830 --> 00:04:09,350
I cannot invoke this one using parentheses as that.

57
00:04:09,360 --> 00:04:15,210
Then I'll pass in my argument two and two, which is full if I save it.

58
00:04:15,210 --> 00:04:23,430
Now you see that I have four a function returning a function that is one example.

59
00:04:24,180 --> 00:04:32,250
And for this one we can call this function also in another way, let's say another way.

60
00:04:34,200 --> 00:04:35,580
Of calling.

61
00:04:37,120 --> 00:04:38,500
This function.

62
00:04:38,800 --> 00:04:39,550
Have a look.

63
00:04:39,950 --> 00:04:40,790
Okay, let's see.

64
00:04:40,810 --> 00:04:45,420
So, first of all, I'll call my my other function as that.

65
00:04:45,430 --> 00:04:48,540
And this function is not taking in anything.

66
00:04:48,550 --> 00:04:49,180
Right.

67
00:04:49,180 --> 00:04:50,230
No argument.

68
00:04:50,230 --> 00:04:57,370
But a second function is that now pass in to comma two as let me comment this one out.

69
00:04:57,400 --> 00:05:03,820
Now if I save it, nothing being displayed because there is pounds coming back from this one.

70
00:05:03,820 --> 00:05:05,980
We need to save it into a variable.

71
00:05:06,070 --> 00:05:09,730
So counts, let's say answer is equal to that.

72
00:05:10,000 --> 00:05:14,790
If I console.log the answer, I'm going to get four as before.

73
00:05:14,800 --> 00:05:18,670
So it is how we can also call the function great.

74
00:05:19,030 --> 00:05:26,320
The next attribute of first class citizen is we can assign a function to a variable.

75
00:05:26,710 --> 00:05:27,340
Let's see.

76
00:05:27,340 --> 00:05:31,660
So now we know that we cannot return a function from a function.

77
00:05:31,660 --> 00:05:34,180
So this is what you call first class citizen.

78
00:05:34,550 --> 00:05:45,160
Now, next, I'm going to be assigning a function to a variable, let's say assign function to a variable.

79
00:05:46,420 --> 00:05:47,230
Let's see.

80
00:05:47,410 --> 00:05:47,860
Great.

81
00:05:47,860 --> 00:05:52,910
So let's say I have a function, and this function is not taking in any agreement.

82
00:05:52,960 --> 00:05:53,970
Just display.

83
00:05:53,980 --> 00:05:56,260
Say, how are you?

84
00:05:57,330 --> 00:06:00,810
So let's see how we can assign a function to a variable.

85
00:06:00,810 --> 00:06:02,610
So let's declare a variable.

86
00:06:02,610 --> 00:06:05,370
Say, answer our results.

87
00:06:06,710 --> 00:06:09,650
Now is equal to say hello.

88
00:06:09,770 --> 00:06:12,770
Because I'm assigning a function to a variable.

89
00:06:12,800 --> 00:06:14,750
I don't need to call it as this.

90
00:06:14,750 --> 00:06:15,170
Right.

91
00:06:15,170 --> 00:06:16,980
So I remove my parentheses.

92
00:06:17,000 --> 00:06:22,610
Now I have assigned my function to a variable called result.

93
00:06:22,820 --> 00:06:24,770
So if I call results.

94
00:06:26,090 --> 00:06:27,140
The results.

95
00:06:27,960 --> 00:06:30,750
Let's see if we're going to get the response.

96
00:06:30,750 --> 00:06:32,130
Indeed we have.

97
00:06:32,160 --> 00:06:33,120
How are you?

98
00:06:33,480 --> 00:06:36,440
And if I call, say hello to.

99
00:06:37,080 --> 00:06:38,010
We also have.

100
00:06:38,010 --> 00:06:39,130
How are you?

101
00:06:39,180 --> 00:06:43,470
You can see we can assign a function to a variable.

102
00:06:43,470 --> 00:06:46,380
So this what you call first class citizen?

103
00:06:46,890 --> 00:06:52,500
Well, we can also save or store a function inside an array.

104
00:06:52,590 --> 00:06:53,780
So let's see.

105
00:06:53,790 --> 00:06:54,300
Great.

106
00:06:54,300 --> 00:06:57,990
So assign or store functions in an array.

107
00:06:57,990 --> 00:07:00,600
Let's say store function.

108
00:07:01,530 --> 00:07:03,240
E I read.

109
00:07:04,110 --> 00:07:12,040
Is that so let's create two functions and let's see how we can save them inside an array or a store

110
00:07:12,040 --> 00:07:14,310
or save function in an array.

111
00:07:14,320 --> 00:07:18,940
So let's say I have a function and this one says, say hello.

112
00:07:19,990 --> 00:07:21,790
And then console.log.

113
00:07:21,820 --> 00:07:22,780
Indeed.

114
00:07:23,200 --> 00:07:23,920
Hello.

115
00:07:24,490 --> 00:07:35,200
And then second function is going to say hi, say hi here, and then just console.log here as high grade.

116
00:07:35,230 --> 00:07:40,450
So for this one, we can save them or store them inside an array.

117
00:07:40,480 --> 00:07:40,990
Let's see.

118
00:07:40,990 --> 00:07:43,900
So const my array equal to that.

119
00:07:44,020 --> 00:07:49,270
Then I will take my first function, say hello comma and then say hi as that.

120
00:07:49,270 --> 00:07:51,190
So how can I call this function?

121
00:07:51,310 --> 00:07:54,280
Remember, I can call it using the indexes.

122
00:07:54,280 --> 00:07:58,480
So array, first index which is hello, this one.

123
00:07:58,600 --> 00:08:03,610
When I save it, you see, we don't see the result here because I have no call the function so we need

124
00:08:03,610 --> 00:08:04,960
to call it as that.

125
00:08:04,960 --> 00:08:06,580
And now we see.

126
00:08:06,580 --> 00:08:07,380
I see.

127
00:08:07,390 --> 00:08:08,020
Hello.

128
00:08:08,140 --> 00:08:16,390
I can also take the second index and then also call it as that is going to be as high.

129
00:08:16,900 --> 00:08:20,320
That's why we is that functions are first class citizens.

130
00:08:20,320 --> 00:08:25,540
Then this video let's talk about high or the functions with example.

