JavaScript functions
This post is for myself to remember the different types(declarations, expressions, arrow)of functions syntax and their difference.
Function is a piece of codes that we can reuse over and over again in your code. It is a bit more like a variable but the difference is a variable holds value and a function holds one or more complete lines of code.
// Function Declarations
// Function Expressions
This is basically a function without a name. This function without a name is an expression and an expression produces a value so we use that value and store it into a variable then, that variable now holds the function value.
In JavaScript, function is a value just as a number or a string or a boolean.
another big difference between these two functions is that you can call function declarations before they are defined in the code like this
but you cannot call function expressions before they are defined
then, you get this initialization error.
This is because of a process called hoisting but I will go deep into that in the future post.
// Arrow functions
A third type of functions is called Arrow functions. This is good for simple one liner functions and it does not even have to write return keyword and it works the same as the other functions but if the function is more complicated with more parameters then there is no advantage of using this arrow functions.
It is more of personal preferences what function type to use besides the arrow function do not get a ‘This’ keyword.