DEV Community

Cover image for Function js JavaScript advance principles
Edeke Emmanuel
Edeke Emmanuel

Posted on • Edited on

Function js JavaScript advance principles

Conditional function

Function can be declare in a conditional statement.

Image description

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

Callback function

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

Image description

with function expression

Image description

with more function expression

Image description

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]
Enter fullscreen mode Exit fullscreen mode

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

Rest parameters

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

Image description

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
Enter fullscreen mode Exit fullscreen mode

With two parameter

Image description
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

without parameter

Image description

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

Function Composition

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

Image description

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

unary function

Image description

multiple nested function

Image description

Top comments (0)