As shown in the W3Schools Function guide
You can use a function declaration or a function expression.
Both are the same in general. Thought there are certain things to consider:
- In a function expression the function is only defined when that line in code is reached
- In a function decleration the function is defined as soon as the code is run, so it can be called before it is written in your code (due to hoisting)
The first example you wrote is a function Declaration
The second example you wrote is a function Expression
The only difference between the two is how you will invoke them in your code after you defined them.
If you are wondering which is better, it all comes down to which you are more comfortable with and think the syntax for is more readable, also try not to mix up the two to keep conventions in your code.