WEBVTT

00:00.900 --> 00:05.970
In the previous video, we have seen how to create functions this way of creating function is called

00:05.970 --> 00:11.610
function statement, but there is another way of defining functions, which is called function expression.

00:12.420 --> 00:17.520
Before we actually see what the function expressions are, let's consider what expression generally

00:17.520 --> 00:18.450
means in JavaScript.

00:19.350 --> 00:25.020
For that will be better to compare expressions and statements, you already know what statements are,

00:25.260 --> 00:31.380
statements or instructions to be executed by the Web browser and statement doesn't produce an immediate

00:31.380 --> 00:31.740
value.

00:32.490 --> 00:38.580
As for the expression, it produces a value and can be written wherever that value is expected.

00:39.650 --> 00:40.130
All right.

00:41.640 --> 00:44.490
We have already learned what if statement is.

00:45.840 --> 00:50.070
It uses conditions and the block of code.

00:51.280 --> 00:57.190
It tells Browsr how to act in different cases and situations, depending on whether condition is true

00:57.190 --> 00:57.780
or false.

00:58.820 --> 01:04.490
So if is a statement, and as we said, the statement itself does not produce a value.

01:06.020 --> 01:12.890
All right, now let's use counsel to demonstrate what expressions are right, a equals two, five plus

01:12.890 --> 01:13.280
five.

01:14.830 --> 01:22.420
It's 10:00, this is an expression because it gives us values that right 10 triple equals to a.

01:24.460 --> 01:28.430
It's an expression, too, because it gives us true, which is a prudent value.

01:29.440 --> 01:34.510
So, again, statement is an instruction to the browser and it doesn't give us a value.

01:34.870 --> 01:37.120
And expression always produces a value.

01:38.980 --> 01:45.130
All right, based on that, now let's consider what function expressions are at first create simple

01:45.130 --> 01:47.560
function and call it add.

01:49.060 --> 01:51.550
That place your parameters and be.

01:52.770 --> 01:58.380
And inside curly braces as a code block writes, consider log A plus B.

02:00.090 --> 02:04.570
This is a standard function declaration and it's called function statement.

02:05.490 --> 02:06.990
So why don't we call it.

02:08.370 --> 02:15.750
With some arguments, five and 10, it gives the Gibralter instruction to execute the code inside a

02:15.750 --> 02:18.960
function like it was in case of if statement.

02:20.040 --> 02:21.480
And we have here 15.

02:22.880 --> 02:25.950
All right, we can define functions in a different way.

02:26.270 --> 02:34.370
I mean, to assign them to the variable as a value, it's called function expression, so creates variable

02:34.370 --> 02:35.360
and call it some.

02:36.470 --> 02:41.240
Then assigned to its function with parameters and be.

02:42.290 --> 02:46.430
And then, right, Castledare, look, A plus B.

02:48.720 --> 02:54.600
So here this is a function statement and this is a function expression.

02:55.910 --> 03:01.880
You may notice that I didn't set function for a function expression, this kind of functions are called

03:01.880 --> 03:05.180
anonymous functions because they do not have names.

03:06.440 --> 03:09.650
You may also wonder how can we call the function without name?

03:10.160 --> 03:12.460
And it's possible using the variable name.

03:12.800 --> 03:14.590
So let's call this function right.

03:14.750 --> 03:18.410
Some with the arguments, 10 and 15.

03:20.650 --> 03:23.290
Revolt, and it's 25.

03:25.170 --> 03:30.560
All right, there is one important difference between function expressions and function statements,

03:31.020 --> 03:35.100
that's the first call heds function before function declaration.

03:36.280 --> 03:38.770
Revolt, and it still works.

03:39.940 --> 03:42.220
Now, let's do the same for function expression.

03:44.080 --> 03:45.370
That reload the page.

03:46.430 --> 03:52.070
And you see, we have type error, which tells us that something is not a function.

03:53.410 --> 03:59.200
For now, just remember it, because the reason of that difference is an issue of hoisting, hoisting

03:59.200 --> 04:02.610
is one of the topics regarding how JavaScript works behind the scenes.

04:02.620 --> 04:05.750
And as we said, it will be discussed in coming sections.

04:06.670 --> 04:07.170
All right.

04:07.660 --> 04:11.400
So in this lecture, we have discussed how to define functions in different ways.

04:12.220 --> 04:15.640
What is difference between function statements and function expressions?

04:16.180 --> 04:21.940
Functions are more complex things in JavaScript, but at this point, the basic knowledge regarding

04:21.940 --> 04:23.020
them is enough for you.

04:23.960 --> 04:25.660
OK, let's go ahead.
