In the previous video, we have seen how to create functions this way of creating function is called
function statement, but there is another way of defining functions, which is called function expression.
Before we actually see what the function expressions are, let's consider what expression generally
means in JavaScript.
For that will be better to compare expressions and statements, you already know what statements are,
statements or instructions to be executed by the Web browser and statement doesn't produce an immediate
value.
As for the expression, it produces a value and can be written wherever that value is expected.
All right.
We have already learned what if statement is.
It uses conditions and the block of code.
It tells Browsr how to act in different cases and situations, depending on whether condition is true
or false.
So if is a statement, and as we said, the statement itself does not produce a value.
All right, now let's use counsel to demonstrate what expressions are right, a equals two, five plus
five.
It's 10:00, this is an expression because it gives us values that right 10 triple equals to a.
It's an expression, too, because it gives us true, which is a prudent value.
So, again, statement is an instruction to the browser and it doesn't give us a value.
And expression always produces a value.
All right, based on that, now let's consider what function expressions are at first create simple
function and call it add.
That place your parameters and be.
And inside curly braces as a code block writes, consider log A plus B.
This is a standard function declaration and it's called function statement.
So why don't we call it.
With some arguments, five and 10, it gives the Gibralter instruction to execute the code inside a
function like it was in case of if statement.
And we have here 15.
All right, we can define functions in a different way.
I mean, to assign them to the variable as a value, it's called function expression, so creates variable
and call it some.
Then assigned to its function with parameters and be.
And then, right, Castledare, look, A plus B.
So here this is a function statement and this is a function expression.
You may notice that I didn't set function for a function expression, this kind of functions are called
anonymous functions because they do not have names.
You may also wonder how can we call the function without name?
And it's possible using the variable name.
So let's call this function right.
Some with the arguments, 10 and 15.
Revolt, and it's 25.
All right, there is one important difference between function expressions and function statements,
that's the first call heds function before function declaration.
Revolt, and it still works.
Now, let's do the same for function expression.
That reload the page.
And you see, we have type error, which tells us that something is not a function.
For now, just remember it, because the reason of that difference is an issue of hoisting, hoisting
is one of the topics regarding how JavaScript works behind the scenes.
And as we said, it will be discussed in coming sections.
All right.
So in this lecture, we have discussed how to define functions in different ways.
What is difference between function statements and function expressions?
Functions are more complex things in JavaScript, but at this point, the basic knowledge regarding
them is enough for you.
OK, let's go ahead.