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
JavaScript, Function
- JS: Define Function
- JS: Arrow Function
- JS: Function Parameters
- JS: Function arguments Object
- JS: Function Rest Parameters
- JS: Function Parameter Default Value
- JS: Function Argument Destructure
- JS: Function. Declaration vs Expression
- JS: Closure
- JS: Function Call, Apply, Bind
- JS: Functional Programing
- JS: Function Pipe 💠
- JS: Function Object
- JS: Function Constructor
- JS: Function.prototype