Let and const are two relatively new features in javascript, another cool new feature are arrow functions.
We can rewrite this function as an arrow function by first of all creating a new variable or constant,
since I'll never set a new value, I'll use a constant
and I'll give that the name of my function,
so summarize user. The value which I then assign after the equal sign is a function.
Now we could already have done that in the past with this syntax, this is another way of defining a function,
the part on the right side is a so-called anonymous function because we don't set up a name after
function but we make it a named function implicitly by storing the anonymous function in that named
constant so we can always call that constant which holds a function as a value and we call the value
with the syntax and therefore this is like a named function here.
So this would have worked in the past too,
it is a way or a different way of defining a function but we can now also use a new syntax where we remove
the function keyword and instead we add an arrow between the argument list and the curly braces and
this arrow is simply an equal sign and an greater than sign.
This also creates a function,
it's a bit shorter since we save the function keyword and it runs in the same way as this function ran
before.
Now why would we use the syntax except for the reason that it's a bit shorter?
Well that is already a nice reason but also there's one key difference regarding the this keyword which
javascript knows and attached you find a link to another video and article I created where I dive into
the this keyword and how arrow functions help us with it.
The attached article and video actually uses the browser but it's the same for nodejs.
So this will be helpful too,
you have to know what javascript objects are for that but again that is some core knowledge I require
you to have for this course. In this course
I will pretty much only use arrow functions,
so this syntax for defining a function should be something you understand for this course,
that is the name of the function
and then here we have the arguments and then we have the function body.
Now a little side note, there is also a shorter syntax of writing this or a couple of shorter syntaxes.
Let's say I have another function which I'll name add
and there I get two arguments A and B and I just returned the sum of them so addition.
Then you could write return A plus B of course and that allows us to run console log,
add with 1 plus 2
and if I now run node play.js, we see three here
as a result. If you only have an arrow function with one statement which happens to be the return statement
or which you are fine returning,
well then you can omit the curly braces,
you can omit the return step and you have to omit it and you just use the function like that.
This simply is the same syntax as before with the curly braces and with return and this function will now
always return the result of this statement here.
So now if I execute this again, we still see three.
Now if I would adjust this function to let's say always add one,
then I have just A as an argument
let's say and I return A plus one.
Now I could call this by console logging
add 1 to let's say 1 here, so the result should be 2 and I can of course execute this and indeed I see
two here. Now in such a case you already saw my auto-formatting removed the parentheses I had there previously
because if you only have one argument and that's the case really for that case only, if you have one
argument only then you can just have the argument name here without parentheses and it will work just
as it will work with parentheses,
so both works here but you typically use the syntax without the parentheses
and again, my IDE auto-formatting
remove them for me.
If you have an arrow function with no arguments at random, then you have to specify an empty pair of
parentheses though, you can't have just a whitespace,
you have to have that empty pair and then you can have your code there which obviously uses no arguments
because that's exactly what I want to show here.
So here I could have at random called like this without any data passed in
and now we see three here from that result.
So these are arrow functions and their different syntaxes and you will see them throughout the
course and you should recognize that syntax and understand how they work.
Again for a reason why to use them,
check out the attached article plus video.