You already know that in JavaScript we have function statements and function expressions in case of
function expressions, you're able to assign function as a value to the variable and then invoke it
using variable name.
It's because functions in JavaScript are first class functions.
So I'm not going to repeat again what function expressions are because you already know well how they
work.
I'm just going to show you real power of JavaScript functions which actually act in different ways than
in many other programming languages.
So what are first class functions?
This concept means the following that in JavaScript we are able to store function in a variable we can
use function as a variable, also pass them as parameters and we are able to return function from other
function in many other programming languages.
We are not allowed to use functions in such flexible ways.
All right, let's see exactly what I'm talking about.
So as we said, we can store functions in variables that's demonstrated once again, we need to create
variable and assign to it anonymous function.
So right.
VAR A equals to anonymous function.
And then right, counsel Deathlok.
Hello.
In order to invoke it, we can use the name of the variable and we need to write it after verbal declaration
because of the hoisting, if we invoke it before it's declaration.
Then we will get an error.
Because as we already know, the value of the variable A is undefined.
OK, it was just for reminding some important things.
Let's write another example and remove this code, create function and call it some.
Inside the parentheses.
Let's consider the parameters, number one and number two.
And now I'm going to pass here a third parameter, which I will use then as a function, I mean that
when we invoke function some, we will pass this function as an argument.
So right here from.
Then in council, that's ad number one and number two, right?
Good luck, number one.
Plus number two.
Then write another consider log in, which I'm going to invoke function, which is passed as a parameter.
So right here and.
OK, let's invoke function some rule that we need to write some.
And then pass arguments for number one and number two as five and 10, and now I'm going to pursue as
an argument, anonymous function.
So right to function and then inside the coal prices return statement and write function is executed.
Reload and you see that we have 15 and that function is executed, so you see that we have post function
as a parameter for another function.
OK.
As we said, we can use function as a variable, and in order to do that, we can define this function
outside.
Let's cut it from here and.
Paste, then call it as done.
And then as an argument, we need to pass your name of the function as variable
reload, and you said that we have again, 15 and function is executed.
All right, in order to make our election more interesting, I'm going to write another a bit complicated
example.
Suppose that we have an array which consists of scores of students so.
Right.
Of course, equals and plus here, some numbers, 55, then 35, 87, and let's say 45.
Now we need to create a function which will check if a particular student could pass an exam.
Keep in mind that the limit of passing exam is 51 points.
Recently, I have shot the example like this, but in this case, I'm going to tweet function as first
class function.
So write your function and call it check result inside the parentheses.
I'm going to pass parameters first one for array and the second one for function.
Then inside the the braces create another array which will be empty for now because I'm going to add
in that array results.
So write a new array equals to empty square brackets.
In order to check a particular score, we need to look through the array items for that, I'm going
to use for a loop.
So right for.
Then variable I equal to zero.
Then we need I is less than I write at length.
And then the increment I plus plus, OK, in order to add items, the new empty array, you remember
that we need to use a right push method.
So right newera that push.
And in a moment of adding items, we need to invoke function, which was passed as a parameter here,
as an argument for the function, we need to pass current item from the array.
So right then and inside the parenthesis right away and in square brackets indicate the variable.
And after that, in order to get a new era, we need to return it.
So.
Right.
Return a new array.
OK.
Now, let's write function, which will actually check if a student could pass an exam and then this
function will be passed as an argument when we call check result function.
So write function and call it pass or fail.
As you see, we have passed your parameter, which points to current item, and now we need to indicate
it here as an argument.
Let's call it score.
In order to pass the exam, the current score should be great or equal to 51, so let's write it.
Return score is greater than or equal to 51.
All right, now it's time to check results function.
Write it in console console that lock check result.
So this function has two parameters, array and function as an array.
We need to process course and as function, we need to use pass or fail function that we have just created.
Reload and you see that we have a right which consists of boolean values, true value means that score
was greater or equal to 51 and the student could pass an exam.
And in case of false, the student was failed.
All right.
We can create multiple functions, which will give us other information about the result of the exam,
for example, if we want to define what was the difference between the limit and the actual scores,
we can create another function.
So right function.
And call it calculate difference that inside the parenthesis, right, score again and as a written
statement, we need to write a score minus fifty one.
Then duplicated and changed the name of the function into calculate difference.
Reload, and so here we have another array, which consists of differences between 51 and the actual
scores.
OK, it's nice, right?
Using functions as first class functions is a big advantage of JavaScript because they are very flexible
and convenient way to write really nicely structured code.
All right, let's move on.