What is a Function ?
A Function is a block of code that is designed to perform a task and executed when it is been called or invoked.
- Function Declaration
- Function Expression
- Arrow Function
1. Function Declaration: Function Declaration is the traditional way to define a function. It is somehow similar to the way we define a function in other programming languages. We start declaring using the keyword “function”. Then we write the function name and then parameters.
Below is the example that illustrate the use of Function Declaration.
After defining a function, we call it whenever the function is required.
Below is the example that illustrate the use of Function Expression.
Here, the whole function is an expression and the returned value is stored in the variable. We use the variable name to call the function.
Below is the example that illustrate the use of Arrow Function.
This shortens the code to a single line compared to other approaches. In a single line of code, the function returns implicitly.
Note: When there is a need to include multiple lines of code we use brackets. Also, when there are multiple lines of code in the bracket we should write return explicitly to return the value from the function.
b is greater