Instructor: In this video you will
learn about function statements and expressions.
So, in the last video where we first talked
about functions I always declared functions
using function statements, but there's actually
another way of writing functions in Java Script,
and that is by using function expressions, okay.
So, all the logic of functions that I explained to you
in the last lecture still work the exact same way.
We just write the function in a slightly different way,
and under the hood of Java Script
it also works quite differently actually,
but for now that's not important for us.
I just want you to know at this point
that we have both function statements
and expressions, so let me show it to you.
And so, when we write a function expression we start
by writing the function basically without the name,
so we still use the function keyword
and then right after that we put
the arguments list, and so this case
I want to paste in the job, enter first name.
Okay, and only then we basically
assigned this function to a variable.
So, we declare a variable, and this one
I'm going to call, "whatDoYouDo," so basically
it's a function where we paste in the job and then
the function tells us what that person does.
So, "whatDoYouDo," so that is now a variable,
and what we assign to that variable is simply this function.
So, function where we paste in job and first name,
and then of course the function block.
All right, so comparing that to what
we had before, all right, let's write it out here.
So, that is a function expression.
And if we did the same thing as a function declaration
then we would write, "Function whatDoYouDo,"
and then the arguments list, so something like this.
Okay, so you see the difference?
Okay, so let me comment this out here.
So, this is just so you know what the difference
between a declaration and an expression is,
and again, the way that we call the function
and the way that we paste in the parameters
and all that stuff that you learned before still works
the exact same way with function expressions.
All right, so next I'll use a switch statement here
so that you can practice some different stuff
that we learned before and we will do something
similar to the switch that we had before actually.
So, we're gonna switch the job here and then
our different cases will be for, say, teacher.
And let's add the cases here before.
So, case teacher, then we have driver.
Then we have designer...
And then finally the default case,
if none of the before apply.
All right, now the other time where we did
this similar switch we had a canceled out log
and then a break, but this time we're
going to actually return something.
So, let's say we want to return first name
plus the string that we had before.
So, "teaches kids how to code."
Okay, and now we will actually
not have to include the break.
That is because of something that I didn't
tell you before, that is when we hit the return keyword
here then we return whatever we define after it,
but what also happens is that
the function immediately finishes.
So, basically we immediately go out
of the function and return to the place
where we actually called the function.
Okay, so the return keywords does not only return the value,
but it also immediately finishes the function.
Okay, so we do not need the break here.
So, in case that the job is teacher,
then we return this string here
and then immediately exit the function.
All right, now in case that the job is the driver,
well, then we return something else.
Okay, and so in that case this line here is never
executed, obviously, and so it's not going
to finish the function and instead it will come
to this next case, which is driver.
So, first name, and let's say, "drives a cab in Lisbon."
Then the same thing for the designer...
"Designs beautiful websites," and then
finally the default case...
Does something else.
Okay, so let's now give it a try.
So, just like before we call our function, "whatDoYouDo,"
and then we paste in the job and a first name.
So, let's paste in, "teacher," and of course, "John."
And don't forget that this function here returns
something, and so we should save that somewhere.
All right, or instead of saving it we can
of course also simply log into the console,
so let's do that instead.
Okay, so that's something that we didn't do before.
So, immediately logged to the console
the result of calling this function.
Okay, but that just works the same.
So, in this case what we will return here is the string,
"John teaches kids how to code," right?
And so that will then be logged to the console.
So, exactly like I said, this is
the string that we expected, right?
And now of course we can do this multiple times.
So, let's say, "designer, Jane," and then...
Well, we can say that, "Mark is retired."
Let's see what happens now, so "Jane designs
"beautiful websites," and then Mark does something else
with just the default clause here.
So, again you see how useful it is to be
able to reuse all of this code.
Otherwise we would have to basically write this here
three times if you wanted to have this scenario
with three people and three different jobs.
And like this we simply paste to
the function the things that change, basically.
So, what's different here is the job
and the name for each of these cases,
and so that's what we paste into the function,
both the job and the name, and then based on
that the function does something, which in this case
produces a string and then returns it.
So, again, we can always see a function
like a machine that receives something,
then does something with the data,
and gives back something different.
Okay, so this is how we write a function expression.
Now, statements and expressions,
they do not only apply to functions,
but they are a global concept in Java Script, actually.
So, before moving on let's talk a bit about
the difference between statements and expressions.
So, Java Script expressions are pieces of code
that always produce a value, and it doesn't matter
how long they are as long as the code results
in a single value, and then it is an expression.
So, let's take a look at that in the console here.
And so, for example, if you simply write, "2+3,"
well, then you see that this produces
a result, so a 5 in this case.
And so, what we just wrote here
in a Java Script expression, okay?
And the same is when we, for example, call this function.
So, let's call this function right here
in the console, and we actually have access
to these functions here right in the console.
Okay, so that's something that we didn't do
up until this point basically using the variables
and function that we define our inner code right
in our console, but we can do that.
Okay, and so when we now call this function here
then you see that of course it returns a value,
and so again, this function call here is an expression.
And really, anything that we do,
again, that produces a result is an expression.
So, for example, "typeof 23," and it gives
us back this result of "number,"
and so again, this is an expression.
All right, so what this means is that
whenever Java Script expects a value
we always have to write an expression.
Okay, and the importance of this whole concept
might be a bit hard to grasp at this point for you,
okay, but it's really important to learn
this stuff for the future, all right?
So, for example, here in this FL statement...
For example, here Java Script of course expects
a value here, and so this here needs to be
an expression, and of course it is because
this operator here will of course
return true or false, and so that is a result
and so, therefore it's an expression.
Okay, now anyway, about statements,
they do just perform actions.
So, they do things but they do not produce
immediate results, so these are things like FL statements,
a why loop that we're gonna talk about later,
or even a function declaration.
They don't produce any immediate value,
and so they are called statements.
So, let's again take a look at that here in the console.
I'm gonna write a very simple FL statement here.
So, let's say "if true," and so this
if block here will of course be entered.
So, let's write console.log here of
just some numbers, some random stuff.
So, it doesn't really matter here,
and so let's hit return, and you see now here
that what this returns here is undefined.
Okay, so it doesn't really return anything,
but this 23 here simply comes from this console.log here,
but the statement itself, it does not return anything.
So, it's just really this statement
that doesn't produce a result, and so that's the difference
between expressions and statement, all right?
So, coming back to functions here,
we learned that function expressions produce
an immediate result, while function declarations do not.
And this will actually be very important
for certain situations that you're gonna
learn about later in the course.
So, in these two lectures we covered the very,
very basics of functions, but of course
there's a whole lot more to learn about functions.
But again, I will leave that for a bit later
in the course, so that we're now ready to move on
to another huge and exciting topic, that is erase.