Function js JavaScript advance principles
Tags: webdev, javascript, beginners, programming
Published: 2023-04-17 22:06:24

## Conditional function
Function can be declare in a conditional statement.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo5n10nm5sevx3p302wj.png)

## Recursive function
This is a function that call itself. Just for instance, looping with `while` loop can run code for infinite number of times but when `break` keyword is applied, it prevent such from happening, likewise recursive function can be infinite.

Using `if`...`else` statement ends infinite recursion.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lst2mzh6tqdh8jbukdc6.png) 

## Callback function
This is simply when an arguments is passed as a function call in another function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtt3fs9mt6nrn3iu0dzh.png) 

#### with function expression 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cey11eqybkl8qjey8qio.png) 

#### with more function expression 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtrmgon2cllt5r0e5oea.png) 

## Array method runs as function

These are some array method like map, reduce, filter, slice ... are use as a function.

```
const number = [10, 20, 30, 40, 50]; 
//adding 100 to each value in number
const result = number.map( 
(num) => num += 100
) 
console.log(number); // [10, 20, 30, 40, 50]
console.log(result); // [110, 120, 130, 140, 150]
```

Review my article on all JavaScript method; array methods...

Click now 👆

## Arguments in function call

It's when many argument are pass to a function along with a seperator like **, ; : - _** using `arguments[i]`; this simply means array of argument from 0 to any positive number.

It have a similarity in rest parameters. To get the total number of arguments passed use the keyword `arguments.length`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cigmjz1niyzfpptww5b.png) 

## Rest parameters
This is when a function call many parameter with it parenthesis.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pw6m77fr05rg92939ebk.png) 

## Nested function

Simply refer to as function in a function.

```
function divide(x) {
  function value(y) {
    return x / y;
  }
  return value;
}
console.log(divide(10)(5)); //2
```

#### With two parameter

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e32mpi066yg3po14dr54.png)
It also form a closure, meaning the inner function(second function) can only be accessed with in the outer function and can make use of the arguments and variables of the outer function(first function) while outer function can not access variables or define function inside a inner function.

## Function closure
These are inner function in a outer function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grz622j5lh8vvizhyma3.png)
#### without parameter 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17x3qaa18ysdbs49cqny.png) 

## Function Curry
This is a sequence of `function` that runs each containing a single parameter and then return many `argument`.
It's `function` that return other `function` with single parameter each. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eeiqqrtqay5o003jhgbb.png) 

## Function Composition
This is applying more than one function to a function call.
It's shows similarities to mathematical derivations in function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2aglpe0hgqdghohgkcrk.png) 


## Function Decorator
This is a `function` that takes one `function` as an argument and return that `function` with variation of argument. 
This is refers to as a design pattern, that wraps function in another function. 
It can also be use in Class decorator and Class member decorators. It is deep in learning and widely used in TypeScript, for to be use in JavaScript needs some tools like Babel and TypeScript compiler.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fztxj0rcltfl2kk5gczq.png)
#### unary function

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pewapm7pgptnp3u0lds.png)

## multiple nested function 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxla8g99bv6zwoh8kacn.png)