JS: Define Function
Create a Function
There are 3 ways to create a function.
- By the keyword
function. - By Arrow Function. Most simple.
- By Function Constructor keyword
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.
function ff(x) { return x + 1; } console.log(ff(2)); // 3