JavaScript: Define Function
There are 3 ways to create a function.
- By Arrow Function expression. (JS2015)
- By the keyword
function
. Either as expression or as declaration. - By constructor with keyword
Function
. (good for creating function at run-time.)
Arrow Function
Arrow function is the most simple and useful.
Example:
((x,y) => x + y)
Arrow function is a expression. It is a value that represents the function. You can give it a name by assigning it to a variable.
[see Arrow Function]
Keyword βfunctionβ
The syntax is:
function name (parameters) {body_statements}
Return Statement
In the function body, you can have
0 or more
return expr
.
When program reaches the return statement, it'll exit the function, and return the value.
Function without return statement returns the value undefined
.