[MUSIC].
>> Hi. My name is Aaron Paul.
I'm a Principal Cloud Advocate here at Microsoft.
Today, we're going to have a look at some demos of how we can use
the arrow function or
the fat arrow function as you might also know it as in JavaScript.
I'm going to start by declaring
a fat arrow function in JavaScript.
Now, we have to do that by assigning it to a variable.
Let's create one and we'll call it add,
which will take two numbers and add them together.
We'll take two arguments.
Then we use our arrow characters,
so which is the equal sign and then the greater than symbol.
In my text editor and use the font that I've got,
it actually looks like an arrow and this
is where it gets its name, fat arrow.
Then in this will do add A and B,
so using the plus operator to combine them together.
Now, we can do console.log.
This is the news, just the same way as you would
use a traditional function in JavaScript to add,
I'll do one comma two.
If we save this and then we'll run it from
here in our console and we get,
as we expect, three is the output.
What's interesting about the arrow function that I've defined here
is that it's got an implicit return statement.
I haven't had to use the return keyword to make this happen.
I've just added those two together.
This is because in a single line
JavaScript function that's defined
using the arrow function syntax,
it is going to be treated as an implicit return.
Whatever the result of the operation you've got on this line
is going to be returned as the output of that function.
It doesn't mean you can't do things like
declare a variable with inside of an error function
that doesn't have multi-line feature enabled for it,
because you don't have the ability to do
a variable declaration and have that
returns something that would be invalid syntax.
We can still create
an arrow function that does work across multiple lines.
We create a new function. [inaudible].
I'll call this one, subtract.
We'll take an A and B.
In this case we're going to use
the squiggly brackets to create a block.
Inside of this block, we now can treat it
like we would a traditional JavaScript function.
We can have multiple lines,
so I could do const.
Result is going to be A minus B.
Then we can return using the return keyword and return results.
Now, in a multi-line arrow function,
we have to be explicit about a turn using the return keyword.
This is because it's treated then like
a normal function block and
we don't know where that function might end,
so we use the return keyword in that case.
Also this means that a multi-line JavaScript function
defined with the array syntax can also not return a value.
Well, in true will actually return
undefined implicitly because a function that
doesn't use the return keyword won't returned to
value it will return undefined.
If we do a console.log of subtract, one and one.
Now, if we do console.log and run our demo,
we'll see that we get zero.
We get what we would expect and our function works the
same as we would with any other functions in JavaScript,
just where I would use the arrow syntax
and take some advantages of
some shorter character count in
maybe a response if we're sending this down in
a browser or fewer keystrokes,
if you're a little bit of a slack and developer like myself.
But also we have the ability to do implicit returns,
by having a single line function with
no squiggly brackets around it to denote a function block.
But we can still have multiple lines if we so
desire and we need to insert that function.
That's a quick look at how we can use functions
using the arrow syntax in JavaScript. See you next time.