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:
In questa pagina del sito puoi guardare il video online Part 27 | JavaScript Tutorial | Functions | Self-invoking | Currying functions | Recursive Function della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Trinits Technologies 04 marzo 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 71 volte e gli è piaciuto 1 spettatori. Buona visione!