2.5 fleche function
Hello everyone, I hope you are doing well.
Welcome to this new video where we’re going
This is a different syntax for the creation of
Javascript functions. A normal javascript function looks like
of course to this.
You can also note a syntax where you have var myFunc equal to function.
Now, an arrow function looks like this. Here, I store it in a
constant and then right side
of the equal sign. This is the syntax of the arrow function. A list of arguments is
nothing, but it can contain
0:35 arguments, and then an arrow. So, an equal sign and a sign greater than, and then
the body of function.
The snyntax arrow of the function is a bit shorter than the normal syntax because
she omits the
function keyword and this is also a considerable advantage. Solves many problems
that you have often met with the. If
you worked a little with javascript, you probably know
that the keyword THIS does not always refer to WHAT you could have you
wait for when
writing your code. When you use this function in a
arrow, it will always keep its context and
will not change it
not at run time. Now let’s move to jsbin
Let’s create a normal function, printMyName like this.
Now, here we can of course display the name of the console log
output and name of
passes as an argument.
Now let’s write printMyName
and in it we will put our first name Matthew .
We have Matthew. Now the equivalent of the arrow function is the
store as a constant named
printMyName. It could also be created with let.
If you plan to reassign this printMyName variable as a constant. And
now, don’t forget to add this arrow between
the list of arguments and the body of the function. If you delete and rerun this
operation, you’ll always see
Matthew. So here he behaves in exactly the same way. Now, the
keyword-thing is something you will see
throughout the course. This becomes important once you have added
object functions, of course.
First of all, let me show you some alternatives to this
syntax, regarding the list of arguments. To be precise. If you do not
receive only one argument,
like this If you receive only one argument as we do here, you
can also
Use a shortcut to omit parentheses.
This is only valid for exactly one argument, not for more and not for less.
Here you will always get the same result. If you had a function that does not
receives no argument. You
need to
You also need brackets.
So, name and age like that won’t work.
You’ll have to wrap this in parentheses. And now you can write name
and age and pass both as arguments
here. So these are two different syntaxes that you might see regarding
arguments. There is also an alternative.
So on the right side of the arrow. Obviously, a lot of functions
return something. So let’s say here that we really want to multiply
some
thing and that we get a number as argument. We now want to return
the number * 2.
Of course, what
we can do, it’s that we can
record the result of the multiplication. Therefore, what is returned
by
multiply and pass two here. If now I delete this and throw it, we
should see four if you
have this case where everything you do in the body of your function is returned and
that you
have no other code, you can omit the braces, write it on a line and
the keyword return as well. It is a
very short version
of writing this function. It gets a little shorter if we take advantage
the shortcut to delete
the parentheses surrounding the single argument.
And now it always returns the result of this code. Here, we omit
just the keyword back and we
have omitted two.
And we have a very concise and short way of writing a function that takes one or
several arguments
and flip something.
So, here, delete and execute this still gives 4. So it’s all the syntax that
you can see and the
function arrow in general is something you
will often see. You have seen the different
syntaxes. You are not required to
remember everyone now.
Just know that there are different syntaxes
and that if we use them afterwards,
they will come back to you quickly and
you will know why we use a given syntax.
thank you for watching we meet on the next course