Hi and welcome to the JavaScript Video course.
In this lesson we'll be exploring arrow functions.
Arrow functions are a new way to declare functions in JavaScript.
They are shorter, more concise and provide a new way to handle this keyword.
For example, let's consider the following
code. Let's clean up our file and let's create function myFunction
that takes in two arguments A and B and outputs their sum.
We can rewrite the above function as
an arrow function as follows. const myArrowFunction
it will take in arguments A and B and it will return their sum.
So let's run these functions.
Let's say we run my function with one
and two and my arrow function with one and two and let's console log the results
and run this and we see that both of them give us the value of three.
You can rewrite the arrow function
in a shorter way, which would just be like this.
If we rerun this, you will see three outputted again.
So you would only do this if you have a single statement.
If you happen to have multiple statements in your functions and there's multiple
things that are happening, you would still need to do this.
And on other lines you can write your other logic.
If you have an arrow function with no
arguments, then you have to specify an empty pair of parentheses.
Something like this for example:
let's say this is random function that returns three if you want
to run this function actually, let's make it return ten.
If you want to run this function, you would have to do this.
So let's run this.
And now we see ten as well in our console log.
These are just a few examples
of the different arrow function syntaxes and you will see them throughout this
course and it is important that you understand and recognize them.
In conclusion, arrow functions are a great
feature in JavaScript that allows us to write cleaner, more concise code.
Thanks for watching and have a great day.