Part 27 | JavaScript Tutorial | Functions | Self-invoking | Currying functions | Recursive Function

Publicado el: 04 marzo 2023
en el canal de: Trinits Technologies
71
1

Functions:-
---------
1. Self-invoking functions
2. Function default parameters
3. Rest operator in JS
4. Currying functions
5. Recursive functions

1. Self-invoking functions:-
---------------------------
Functions are calling them self are called self-invoking functions.
Self-invoking function will be called Automatically.
You have to add parentheses around the function to indicate that it is a function expression
A function without a name called an anonymous function.

(function(){
console.log('Hello guys ready for interview');
})();


2. Function default parameters:-
-----------------------------
If a function is called with missing arguments (less than declared), the missing values are set to undefined.
function add(x, y){
return x + y;
}
//Since we are passing only 1 value, the result will be NAN. Because 2nd value will be undefined.
add(10);

function add(x, y = 20){
return x + y;
}
console.log(add(10));

3. Rest operator in JS:-
--------------------
The rest operator in JavaScript is represented by three dots (...) and is used to represent an indefinite number of arguments as an array. It allows you to pass any number of arguments to a function and have them be collected into an array.

In this example, the sum function takes any number of arguments using the rest operator (...numbers). The rest parameter numbers collect all the arguments passed to the function and store them as an array.

4. Currying functions:-
-----------------------
Currying is a technique that involves transforming a function that takes multiple arguments into a series of functions that each take a single argument.

sum(1)(2)(3) is an example of a function that takes multiple arguments, but each argument is passed in separately using a series of function calls.

function sum(a) {
return function(b) {
return function(c) {
return a + b + c;
}
}
}

console.log(sum(1)(2)(3)); // Output: 6

5. Recursive functions:-
-----------------------
A recursive function is a function that calls itself repeatedly until it reaches a certain condition, also known as a base case, that stops the recursion. Here are some examples of recursive functions in JavaScript:

Factorial function: The factorial of a positive integer n is the product of all positive integers from 1 to n. The factorial of 0 is 1. Here's a recursive function to calculate the factorial of a number:


En esta página del sitio puede ver el video en línea Part 27 | JavaScript Tutorial | Functions | Self-invoking | Currying functions | Recursive Function de Duración hora minuto segunda en buena calidad , que subió el usuario Trinits Technologies 04 marzo 2023, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 71 veces y le gustó 1 a los espectadores. Disfruta viendo!