All About JavaScript Function
Khan Rabiul
Posted on September 12, 2024
What We Will Learn?
- What Is Function
- Terminologies
- Different Types of Functions
- Function Declarations
- Function Executions
- Parameter
- Call Stack
- Nested Functions
- Functions Scope
- Closure
- Callback Function
- Higher Order Function
- Pure Function
- IIFE
- Recursion
đ§âđģ āĻāĻžāĻāĻžāĻ¸ā§āĻā§āĻ°āĻŋāĻĒā§āĻ function
āĻā§?
function
āĻšāĻ˛ā§ āĻāĻāĻāĻŋ āĻā§āĻĄ āĻŦā§āĻ˛āĻ āĻ¯āĻž āĻāĻāĻāĻŋ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻ°ā§āĻŽāĻ¸āĻŽā§āĻĒāĻžāĻĻāĻ¨ā§ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšā§āĨ¤ function
āĻāĻāĻŦāĻžāĻ° define āĻāĻ°āĻž āĻšāĻ˛ā§ āĻŦāĻžāĻ° āĻŦāĻžāĻ° āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤ āĻāĻ¤ā§ āĻā§āĻĄ āĻĒā§āĻ¨āĻ°āĻžāĻŦā§āĻ¤ā§āĻ¤āĻŋ āĻšā§āĻ°āĻžāĻ¸ āĻĒāĻžāĻāĨ¤
đ§âđģ Functions Terminologies:
đ Defining functions (function āĻ¤ā§āĻ°ā§ āĻāĻ°āĻž):
a. Function Declarations:
āĻ¯āĻāĻ¨ āĻāĻāĻāĻŋ function
āĻā§ keyword
- function
, function
āĻāĻ° āĻāĻāĻāĻŋ āĻ¨āĻžāĻŽ, āĻāĻ āĻŦāĻž āĻāĻāĻžāĻ§āĻŋāĻ parameter
āĻāĻŦāĻ statement
āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ āĻ¤ā§āĻ°ā§ āĻāĻ°āĻž āĻšā§ āĻ¤āĻā§ function declaration
āĻŦāĻ˛ā§āĨ¤ function declaration
, function statement
āĻ¨āĻžāĻŽā§ āĻ āĻĒāĻ°āĻŋāĻāĻŋāĻ¤āĨ¤
statement āĻšāĻ˛ā§ function āĻāĻ° āĻāĻŋāĻ¤āĻ° {// do this task} code āĻŦā§āĻ˛āĻāĨ¤ return
āĻāĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§ function
āĻĨā§āĻā§ āĻāĻžāĻā§āĻā§āĻˇāĻŋāĻ¤ āĻĢāĻ˛āĻžāĻĢāĻ˛, function
āĻ¯ā§āĻāĻžāĻ¨ā§ call/invoke
āĻāĻ°āĻž āĻšā§, āĻ¸ā§āĻāĻžāĻ¨ā§ āĻĒā§āĻ°ā§āĻ°āĻŖ āĻāĻ°āĻž āĻšā§āĨ¤
function functionName (parameter1, parameter2) {
return parameter1 + parameter2;
}
functionName(4, 2); // Output: 6
function name
āĻ¯ā§āĻā§āĻ¨ āĻāĻŋāĻā§ āĻšāĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤ āĻ¤āĻŦā§ function call/invoke
āĻāĻ°āĻžāĻ° āĻ¸āĻŽā§ āĻšā§āĻŦāĻšā§ function name āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻšā§āĨ¤ āĻ¨āĻžāĻŽā§āĻ° āĻļā§āĻˇā§ āĻ
āĻŦāĻļā§āĻ¯āĻ ()
āĻŦāĻž parentheses
āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
function parameters āĻšāĻ˛ā§ function name āĻāĻ° āĻĒāĻ° parentheses āĻāĻ° āĻāĻŋāĻ¤āĻ° variableāĨ¤ āĻāĻāĻžāĻ§āĻŋāĻ āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ°ā§āĻ° āĻāĻ¨ā§āĻ¯ āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ°ā§āĻ° āĻŽāĻžāĻā§ (,) āĻāĻŽāĻž āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻšā§āĨ¤
parameters
arguments value receive **āĻāĻ°ā§āĨ¤
function
āĻĨā§āĻā§ āĻ¯ā§ āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻĒā§āĻ°āĻ¤ā§āĻ¯āĻžāĻļāĻž āĻāĻ°āĻž āĻšā§ āĻ¤āĻž return
āĻā§āĻā§āĻžāĻ°ā§āĻĄā§āĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§ āĻ¯ā§āĻāĻžāĻ¨ā§ function
call āĻāĻ°āĻž āĻšā§, āĻ¸ā§āĻāĻžāĻ¨ā§ āĻĒāĻžāĻ¸ āĻāĻ°āĻž āĻšā§āĨ¤ return
āĻāĻ° āĻĢāĻ˛ā§ function
āĻāĻ° āĻāĻžāĻ āĻļā§āĻˇ āĻšā§āĨ¤ āĻ
āĻ°ā§āĻĨāĻžā§ return
āĻāĻ° āĻĒāĻ° function
āĻāĻ° āĻā§āĻ¨ā§ āĻā§āĻĄ āĻāĻā§āĻ¸āĻŋāĻāĻŋāĻāĻ āĻāĻ°ā§ āĻ¨āĻžāĨ¤
function
call/invoke
āĻāĻ°āĻžāĻ° āĻ¸āĻŽā§ ()
- āĻāĻ° āĻāĻŋāĻ¤āĻ° āĻ¯ā§ value āĻĒā§āĻ°āĻĻāĻžāĻ¨ āĻāĻ°āĻž āĻšā§ āĻ¤āĻžāĻā§ arguments
āĻŦāĻ˛ā§āĨ¤
b. Function Expression:
function āĻā§ function name āĻāĻžā§āĻž declare āĻāĻ°āĻžāĻā§ function expression
āĻŦāĻ˛ā§āĨ¤ Function Expression, anonymous function
āĻ¨āĻžāĻŽā§āĻ āĻĒāĻ°āĻŋāĻāĻŋāĻ¤āĨ¤
const functionExpressionfunction = function (num) {
return num +1;
}
functionExpressionfunction(5); // Output: 6
NB: Function Declaration āĻāĻŦāĻ Function Definition āĻāĻ° āĻŽāĻ§ā§āĻ¯ā§ āĻŽā§āĻ˛āĻŋāĻ āĻĒāĻžāĻ°ā§āĻĨāĻā§āĻ¯ āĻĨāĻžāĻāĻ˛ā§āĻ āĻ¸āĻžāĻ§āĻžāĻ°āĻŖ āĻ āĻ°ā§āĻĨā§ function create āĻāĻ°āĻžāĻā§ āĻŦā§āĻāĻžā§āĨ¤
đ§âđģ Function VS Method (function āĻ Method āĻāĻ° āĻŽāĻ§ā§āĻ¯ā§ āĻĒāĻžāĻ°ā§āĻĨāĻā§āĻ¯):
a. Function:
Function āĻšāĻ˛ā§ āĻāĻāĻāĻŋ āĻ¸ā§āĻŦāĻžāĻ§ā§āĻ¨ code block āĻ¯āĻž āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻāĻžāĻ°ā§āĻ¯-āĻ¸āĻŽā§āĻĒāĻžāĻĻāĻ¨ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤ function āĻā§ code āĻāĻ° āĻ¯ā§āĻā§āĻ¨ āĻāĻžā§āĻāĻž āĻĨā§āĻā§ call
āĻāĻ°ā§ āĻāĻ¨āĻĒā§āĻ āĻĨā§āĻā§ āĻ
āĻāĻāĻĒā§āĻ āĻĒāĻžāĻā§āĻž āĻ¯āĻžā§āĨ¤
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Rabiul")); // Output: Hello, Rabiul!
b. Methods:
Method
āĻ āĻāĻāĻāĻŋ function
āĻ¤āĻŦā§ āĻāĻāĻŋ object
āĻā§ āĻ¨āĻŋāĻ°ā§āĻĻā§āĻļ āĻāĻ°ā§, object
āĻāĻ° property
āĻšāĻŋāĻ¸ā§āĻŦā§ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšā§āĨ¤ āĻ
āĻ°ā§āĻĨāĻžā§ Method āĻ¸āĻŦ āĻ¸āĻŽā§ object āĻāĻ° property āĻšāĻŋāĻ¸ā§āĻŦā§ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻŦā§āĨ¤
const user = {
firstName: "Khan",
middleName: "Rabiul",
lastName: "Islam",
fullName : function () {
return `${this.firstName} ${this.middleName} ${this.lastName}`
}
}
console.log(user.fullName());
đ§âđģ Parameter:
đ a. Default Parameter:
JavaScript Function, parameter
āĻāĻ° value undefined
āĻĨāĻžāĻā§āĨ¤ undefined
āĻā§āĻ˛ā§āĻ° āĻ¸āĻžāĻĨā§ āĻ
āĻĒāĻ°ā§āĻļāĻ¨ āĻāĻ°āĻž āĻ¸āĻŽā§āĻāĻŦ āĻ¨ā§āĨ¤ āĻāĻŦāĻžāĻ° āĻāĻŋāĻā§ āĻā§āĻˇā§āĻ¤ā§āĻ°ā§ āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ°ā§āĻ° āĻā§āĻ¯ā§āĻ˛ā§ āĻ¸ā§āĻ āĻāĻ°āĻžāĻ° āĻĒā§āĻ°ā§ā§āĻāĻ¨ āĻšāĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤ āĻ¸ā§ āĻ¸āĻŦ āĻā§āĻˇā§āĻ¤ā§āĻ°ā§ default parameter
āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤
function multiple (num1, num2 =1) {
return num1 * num2;
}
multiple(5); // Output: 5 * 1 = 5
multiple(5, 2); // Output: 5 * 2 = 10
Parameters āĻāĻ° āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ value (=value) āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ āĻ¯āĻ¤āĻā§āĻ˛ā§ default parameter āĻĒā§āĻ°ā§ā§āĻāĻ¨ āĻ¤āĻ¤ā§āĻā§āĻ˛ā§ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤
đ b. Rest Parameter:
JavaScript rest parameter āĻ
āĻ¸āĻāĻā§āĻ¯ argument āĻā§ array āĻāĻāĻžāĻ°ā§ receive āĻāĻ°ā§āĨ¤ rest parameter āĻŽā§āĻ˛āĻ¤ (âĻāĻ¨āĻžāĻŽ) āĻĻā§āĻŦāĻžāĻ°āĻž āĻāĻ āĻŋāĻ¤āĨ¤
function sum (num1, num2, ...rest) {
const restSum = rest.reduce((total, num) => total + num, 0)
return num1 + num2 + restSum;
}
sum(4,5,6,7,8,10,12);
Rest parameter
āĻāĻ° āĻāĻŋāĻā§ āĻā§āĻ°ā§āĻ¤ā§āĻŦāĻĒā§āĻ°ā§āĻŖ āĻŦāĻŋāĻˇā§āĻ
āĻĒā§āĻ°ā§āĻŦā§ āĻāĻŽāĻžāĻ°āĻž Function Declaration āĻ Function Expression āĻ¨āĻŋā§ā§ āĻāĻ˛ā§āĻāĻ¨āĻž āĻāĻ°ā§āĻāĻŋāĨ¤ āĻāĻāĻžāĻ¨ā§ āĻļā§āĻ§ā§ Arrow Function āĻ¨āĻŋā§ā§ āĻāĻ˛ā§āĻāĻ¨āĻž āĻāĻ°āĻž āĻšāĻŦā§āĨ¤
- āĻāĻāĻāĻŋ
function
āĻ āĻāĻāĻāĻŋāĻŽāĻžāĻ¤ā§āĻ°rest
āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ° āĻĨāĻžāĻāĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§āĨ¤ -
rest
āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ° āĻ¸āĻŦparameter
āĻāĻ° āĻļā§āĻˇā§ āĻšāĻŦā§āĨ¤ -
rest
āĻĒā§āĻ¯āĻžāĻ°āĻžāĻŽāĻŋāĻāĻžāĻ° āĻāĻ° āĻā§āĻ¨default value
āĻĨāĻžāĻāĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§ āĻ¨āĻžāĨ¤
đ§âđģ āĻŦāĻŋāĻāĻŋāĻ¨ā§āĻ¨ āĻĒā§āĻ°āĻāĻžāĻ°ā§āĻ° Functions:
āĻĒā§āĻ°ā§āĻŦā§ āĻāĻŽāĻžāĻ°āĻž Function Declaration āĻ Function Expression āĻ¨āĻŋā§ā§ āĻāĻ˛ā§āĻāĻ¨āĻž āĻāĻ°ā§āĻāĻŋāĨ¤ āĻāĻāĻžāĻ¨ā§ āĻļā§āĻ§ā§ Arrow Function
āĻ¨āĻŋā§ā§ āĻāĻ˛ā§āĻāĻ¨āĻž āĻāĻ°āĻž āĻšāĻŦā§āĨ¤
đ Arrow Function:
Function āĻā§ āĻ¸āĻāĻā§āĻˇāĻŋāĻĒā§āĻ¤ āĻāĻāĻžāĻ°ā§ āĻ˛ā§āĻāĻžāĻ° āĻāĻ¨ā§āĻ¯ arrow function āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšā§āĨ¤
Syntax:
() => expression
param => expression
(param) => expression
(param1, parameter2) => expression
() => {
statements
}
param => {
statements
}
(param1, paramN) => {
statements
}
āĻāĻĻāĻžāĻšāĻ°āĻŖāĻ
const sum = (a, b) => {
return a + b;
};
console.log(sum(5, 10)); // āĻāĻāĻāĻĒā§āĻ: 15
const sum = (a, b) => a + b;
console.log(sum(5, 10)); // āĻāĻāĻāĻĒā§āĻ: 15
const square = x => x * x;
console.log(square(5)); // āĻāĻāĻāĻĒā§āĻ: 25
āĻāĻāĻāĻŋ parameter āĻāĻ° āĻāĻ¨ā§āĻ¯ (), āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻĒā§āĻ°ā§āĻāĻ¨ āĻ¨ā§āĻāĨ¤ āĻāĻŦāĻžāĻ° āĻāĻāĻāĻŋ expression āĻāĻ° āĻāĻ¨ā§āĻ¯, {} āĻāĻŦāĻ return keyword āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§āĻ°āĻŋ āĻ¨ā§āĨ¤
đ§âđģ Nested Function:
āĻāĻāĻāĻŋ function-āĻā§ āĻ¯āĻāĻ¨ āĻ
āĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ function āĻāĻ° āĻŽāĻ§ā§āĻ¯ā§ define āĻāĻ°āĻž āĻšā§, āĻ¤āĻāĻ¨ āĻ¤āĻžāĻā§ Nested Function āĻŦāĻ˛ā§āĨ¤
function outerFunction () {
console.log('outer funciton');
function inner () {
console.log('Inner function');
}
inner();
}
console.log( outerFunction());
// Output: outer funciton
// Inner function
āĻĒā§āĻ°ā§ā§āĻāĻ¨ā§ āĻāĻŽāĻžāĻ°āĻž āĻāĻāĻžāĻ§āĻŋāĻ nested function āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŋāĨ¤
đ§âđģ Function Scope:
function scope āĻāĻŽāĻ¨ āĻāĻāĻāĻŋ āĻ§āĻžāĻ°āĻŖāĻž āĻ¯ā§āĻāĻžāĻ¨ā§ function āĻāĻ° āĻāĻŋāĻ¤āĻ°ā§āĻ° variable āĻā§āĻ˛ā§āĻ° āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ access āĻ function āĻāĻ° āĻŽāĻ§ā§āĻ¯ā§āĻ āĻ¸ā§āĻŽāĻžāĻŦāĻĻā§āĻ§āĨ¤ āĻ
āĻ°ā§āĻĨāĻžā§ function āĻāĻ° āĻŦāĻžāĻāĻ°ā§ variable āĻā§āĻ˛ā§āĻ° access āĻĒāĻžāĻā§āĻž āĻ¯āĻžāĻŦā§āĻ¨āĻžāĨ¤
function outerFunction() {
const a = 10;
function innerFunction() {
const b = 5;
console.log(a); // Logs '10'
function corefunction() {
console.log(a); // Logs '10'
console.log(b); // Logs '5'
const c = 2;
return a + b + c; // Returns 10 + 5 + 2 = 17
}
return corefunction;
}
return innerFunction;
}
const inner = outerFunction(); // Returns innerFunction
const core = inner(); // Returns corefunction
console.log(core()); // Logs '10', '5', and '17'
console.log(a);
console.log(b);
console.log(c);
// Output: Uncaught ReferenceError: b is not defined
function āĻāĻ° āĻŦāĻžāĻāĻ°ā§ variable access āĻāĻ°āĻ¤ā§ āĻā§āĻ˛ā§ ReferenceErro; āĻĻā§āĻāĻžāĻā§āĻā§ āĻā§āĻ¨āĻ¨āĻž variable, function āĻāĻ° āĻāĻŋāĻ¤āĻ° define āĻāĻ°āĻž āĻšā§ā§āĻā§āĨ¤
đ§âđģ Closures:
āĻā§ā§āĻāĻāĻŋ function
āĻāĻ° āĻ¸āĻŽāĻ¨ā§āĻŦā§ā§ āĻāĻ āĻŋāĻ¤ Lexical Environment, āĻ¯āĻž āĻ¨āĻŋāĻ°ā§āĻ§āĻžāĻ°āĻŖ āĻāĻ°ā§ āĻāĻāĻāĻŋ function
āĻāĻ° āĻŽāĻ§ā§āĻ¯ā§ variable
āĻ
āĻ¨ā§āĻ¯ function
āĻā§āĻ˛ā§ access/āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻĒāĻžāĻŦā§ āĻāĻŋ āĻ¨āĻžāĨ¤ āĻ
āĻ°ā§āĻĨāĻžā§, āĻ¯āĻāĻ¨ āĻāĻāĻāĻŋ function
āĻ
āĻ¨ā§āĻ¯ function
āĻāĻ° āĻāĻŋāĻ¤āĻ° āĻāĻ āĻŋāĻ¤ āĻšā§, āĻ¤āĻāĻ¨ āĻāĻŋāĻ¤āĻ°ā§āĻ° function
āĻ¤āĻžāĻ° āĻŦāĻžāĻāĻ°ā§āĻ° function
āĻāĻ° variable
āĻŽāĻ¨ā§ āĻ°āĻžāĻāĻ¤ā§ āĻĒāĻžāĻ°ā§ āĻāĻŦāĻ access/āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤ āĻāĻ§āĻžāĻ°āĻ¨āĻžāĻā§ āĻā§āĻ˛ā§āĻāĻžāĻ°(closure) āĻŦāĻ˛ā§āĨ¤
function outerFunction () {
const outerVariable = 'This is outer variable value';
function innerFunction () {
console.log(outerVariable);
}
return innerFunction;
}
const closuer = outerFunction();
closuer();
// Output: This is outer variable value
āĻĒāĻžāĻļā§āĻ° āĻā§āĻĄ āĻ¸ā§āĻ¨āĻŋāĻĒāĻŋāĻā§ outerFunction āĻāĻ° āĻāĻŋāĻ¤āĻ° outerVariable āĻ¨āĻžāĻŽā§ āĻāĻāĻāĻŋ variable init āĻāĻŦāĻ assign āĻāĻ°āĻž āĻšā§ā§āĻā§āĨ¤ āĻāĻāĻ āĻ¸āĻžāĻĨā§ innerFunction āĻ¨āĻžāĻŽā§ āĻāĻāĻāĻŋ function declare āĻāĻ°ā§ āĻšā§ā§āĻā§āĨ¤ āĻāĻŦāĻžāĻ° innerFunction āĻā§ console.log()/access āĻāĻ°āĻž āĻšā§ā§āĻā§āĨ¤ āĻ¯ā§āĻšā§āĻ¤ā§ Lexical Enviornment āĻ inner function āĻ¤āĻžāĻ° āĻŦāĻžāĻāĻ°ā§āĻ° function āĻāĻ° variable āĻŽāĻ¨ā§ āĻ°āĻžāĻāĻ¤ā§ āĻĒāĻžāĻ°ā§, āĻ¤āĻžāĻ āĻ¯āĻāĻ¨ outerFunction āĻā§ call āĻāĻ°āĻž āĻšāĻ˛ā§ āĻ¤āĻāĻ¨ innerFunction āĻ¤āĻžāĻ° outer function āĻĨā§āĻā§ variable āĻāĻ° value āĻā§āĻ°āĻšāĻ¨ āĻāĻ°āĻ¤ā§ āĻĒā§āĻ°ā§āĻā§āĨ¤
function outerFunction () {
const a = 10;
function innerFunction () {
const b = 5;
function hardCoreFunction () {
const c = 20;
return a + b + c;
}
return hardCoreFunction()
}
return innerFunction;
}
const closure = outerFunction();
console.log(closure())
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§¨āĻ outerFunction Lexical Scope āĻāĻ° āĻāĻŋāĻ¤āĻ° āĻāĻ°ā§ ā§¨āĻāĻŋ function declare āĻāĻ°āĻž āĻšā§ā§āĻā§ innerFunction āĻāĻŦāĻ hardCoreFunctionāĨ¤ outerFunction function āĻāĻ° āĻāĻŋāĻ¤āĻ° a = 10 innerFunction āĻ b = 5 āĻāĻŦāĻ hardCoreFunction āĻāĻ° āĻāĻŋāĻ¤āĻ° c = 20 āĻāĻŦāĻ variable a, b, c variable āĻāĻ° āĻ¸āĻŽāĻˇā§āĻāĻŋ āĻ¨āĻŋāĻ°ā§āĻŖā§ āĻāĻ°āĻž āĻšā§ā§āĻā§āĨ¤ hardCoreFunction āĻāĻ° āĻāĻŋāĻ¤āĻ° variable a āĻāĻŦāĻ b āĻ¨āĻž āĻĨāĻžāĻāĻžāĻ° āĻĒāĻ°āĻ lexical enviornment āĻāĻ° āĻāĻžāĻ°ā§āĻ¨ā§ access āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻā§āĨ¤
function outerFunction () {
const a = 10;
function innerFunction () {
const b = 5;
console.log(a);
console.log(c);
function hardCoreFunction () {
const c = 20;
return a + b + c;
}
return hardCoreFunction()
}
return innerFunction;
}
const closure = outerFunction();
console.log(closure())
āĻāĻ§āĻžāĻšāĻ°āĻŖ ā§ŠāĻ innerFunction āĻāĻ° āĻāĻŋāĻ¤āĻ° variable a āĻāĻŦāĻ c āĻŦāĻŋāĻĻā§āĻ¯āĻŽāĻžāĻ¨ āĻ¨ā§āĨ¤ a āĻ c access āĻāĻ°āĻ¤ā§ āĻā§āĻ˛ā§ a āĻāĻ° value āĻĒāĻžāĻā§āĻž output āĻĒāĻžāĻā§āĻž āĻā§āĻ˛ā§āĻ c variable āĻāĻ° access āĻ¨āĻž āĻĨāĻžāĻāĻžā§, Output REferenceError show āĻāĻ°āĻā§āĨ¤ āĻŦā§āĻāĻžāĻ°āĻž āĻ¸ā§āĻŦāĻŋāĻ§āĻžāĻ°ā§āĻ¤ā§ outerFunction āĻā§ grand_parent, innerFunction āĻā§ parent āĻāĻŦāĻ hardCoreFunction child āĻšāĻŋāĻ¸ā§āĻŦā§ āĻŦāĻŋāĻŦā§āĻāĻ¨āĻž āĻāĻ°āĻž āĻšāĻ˛ā§āĨ¤ child āĻ¤āĻžāĻ° parent, grand_parent variable access āĻĒāĻžāĻāĨ¤ āĻāĻŽāĻ¨āĻāĻŋ child āĻ¸āĻ°āĻžāĻ¸āĻ°āĻŋ grand_parent āĻā§āĻ access āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§āĨ¤ āĻāĻŋāĻ¨ā§āĻ¤ā§ āĻā§āĻ¨ā§ parent āĻ¤āĻžāĻ° child āĻāĻ° variable access āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§ āĻ¨āĻžāĨ¤
āĻ¸āĻšāĻāĻāĻžāĻŦā§ āĻŦāĻ˛āĻ¤ā§ āĻā§āĻ˛ā§ āĻā§āĻ˛ā§āĻāĻžāĻ°(closure) āĻšāĻ˛ā§ āĻ¯āĻāĻ¨ inner function āĻ¤āĻžāĻ° Lexical Environment āĻ outer function āĻĨā§āĻā§ variable access āĻāĻ°ā§āĨ¤
đ§âđģ Callbac Function:
Arguments āĻāĻāĻžāĻ°ā§ āĻāĻāĻāĻŋ function āĻā§ āĻ āĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ function āĻ pass āĻāĻ°ā§, āĻā§āĻ¨ āĻāĻžāĻ°ā§āĻ¯āĻ¸āĻŽā§āĻĒāĻžāĻĻāĻ¨āĻā§ callback function āĻŦāĻ˛ā§āĨ¤ callback function āĻā§ function āĻāĻ° āĻŽāĻ§ā§āĻ¯ā§ invoked/call āĻāĻ°ā§āĻ¤ā§ āĻšā§āĨ¤
synchronous āĻāĻŦāĻ asynchronous ā§¨ āĻĒāĻĻā§āĻ§āĻ¤āĻŋāĻ¤ā§ callback function āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻ¯āĻžā§āĨ¤
function multiplyByTwo(num, callbackFunc) {
var result = num * 2;
callbackFunc(result);
}
function ConLogResult(result) {
console.log(result);
}
multiplyByTwo(5, ConLogResult);
đ§âđģ Higher Order Function:
āĻāĻāĻāĻŋ function, āĻāĻ āĻŦāĻž āĻāĻāĻžāĻ§āĻŋāĻ function arguments āĻĨā§āĻā§ āĻā§āĻ°āĻšāĻŖ āĻāĻ°ā§ āĻ āĻĨāĻŦāĻž āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻšāĻŋāĻ¸ā§āĻŦā§ function āĻā§ return āĻāĻ°ā§, āĻ¤āĻžāĻā§ Higher Order Function (HoF) āĻŦāĻ˛ā§āĨ¤
đ Higher Order Function āĻāĻ° ā§¨āĻāĻŋ āĻŦā§āĻļāĻŋāĻˇā§āĻā§āĻ¯āĻ
Argument āĻ function āĻā§āĻ°āĻšāĻŖ āĻāĻ°ā§āĨ¤
āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻšāĻŋāĻ¸ā§āĻŦā§ function āĻĒā§āĻ°āĻĻāĻžāĻ¨ āĻāĻ°ā§āĨ¤
1. Argument āĻ function āĻā§āĻ°āĻšāĻŖ āĻāĻ°ā§:
function higherOrderFunction (callbackFun) {
callbackFun('It is a higher Order Function');
}
function callbackFun (message) {
console.log(message)
}
higherOrderFunction(callbackFun);
āĻāĻāĻžāĻ¨ā§ higherOrderFunction, call āĻāĻ°āĻžāĻ° āĻ¸āĻŽā§ argument āĻ āĻ āĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ function pass āĻāĻ°āĻā§āĨ¤
āĻā§āĻĨāĻžā§ Higher Order Function āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšā§āĻ
const radius = [4,5,8];
// Calculating Area of the circle
const circleArea = function (radius) {
const result = []; // initialized arra to store output
for(let i = 0; i < radius.length; i++) {
result.push(Math.PI * (radius[i], radius[i]));
}
return result;
}
console.log(circleArea(radius));
// Output: [12.566370614359172, 15.707963267948966, 25.132741228718345]
const diameter = function (radius) {
const result = [];
for (let i = 0; i<radius.length; i++) {
result.push(2 * radius[i]);
}
return result;
}
console.log(diameter(radius));
// Output: [8, 10, 16]
āĻāĻā§ āĻā§āĻˇā§āĻ¤ā§āĻ°ā§ āĻĻā§āĻāĻž āĻ¯āĻžāĻā§āĻā§ radius variable āĻā§ access āĻāĻ°āĻā§āĨ¤ āĻāĻŋāĻ¨ā§āĻ¤ā§ function āĻāĻ° operation āĻā§āĻ˛ā§ āĻāĻŋāĻ¨ā§āĻ¨āĨ¤ āĻāĻŽāĻ¨ āĻā§āĻˇā§āĻ¤ā§āĻ°ā§ āĻāĻŋāĻ¨ā§āĻ¨ operation āĻāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻŋāĻ¨ā§āĻ¨ function āĻ¤ā§āĻ°ā§ āĻāĻ°ā§, āĻ
āĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ function āĻāĻ° argument āĻ pass āĻāĻ°ā§ āĻāĻāĻāĻŋ āĻĒā§āĻ¨āĻ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°āĻ¯ā§āĻā§āĻ¯ function/higher order function āĻāĻ āĻ¨ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŋāĨ¤
const radius = [4,5,8];
// Calculating diameter
const diameter = function (radius) {
const result = [];
for (let i = 0; i<radius.length; i++) {
result.push(2 * radius[i]);
}
return result;
}
console.log(diameter(radius));
// Area clcultion's Operation
const area = function (radius) {
return Math.PI * radius * radius;
}
// Diameter clcultion's Operation
const diameter = function (radius) {
return 2 * radius;
}
// Making a function that can calculate area, diameter, and other oprations. It will be a resuable function
const calculate = function (radius, operation) {
const output = [];
for(i = 0; 0 < radius.length; i++) {
output.push(operation[i]);
}
return output;
}
console.log(calculate(radius,diameter));
console.log(calculate(radius,area));
āĻ§āĻžāĻĒ ā§§āĻ āĻāĻāĻāĻŋ function āĻ¤ā§āĻ°ā§ āĻāĻ°āĻŋ, āĻ¯ā§āĻāĻŋ āĻāĻāĻāĻŋ value āĻāĻŦāĻ āĻāĻāĻāĻŋ function āĻ°āĻŋāĻ¸āĻŋāĻ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§āĨ¤
āĻ§āĻžāĻĒ ā§¨āĻ āĻĒā§āĻ°āĻ¤ā§āĻ¯ā§āĻāĻāĻŋ āĻāĻ˛āĻžāĻĻāĻž operation āĻāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻŋāĻ¨ā§āĻ¨ āĻāĻŋāĻ¨ā§āĻ¨ function define āĻāĻ°āĻŋāĨ¤
āĻ§āĻžāĻĒ ā§ŠāĻ HoFs āĻā§ call āĻāĻ°āĻžāĻ° āĻ¸āĻŽā§ āĻĒā§āĻ°ā§ā§āĻāĻ¨ā§ā§ operation/function āĻā§ pass āĻāĻ°āĻŋāĨ¤ āĻāĻāĻžāĻ¨ā§ value = radius āĻāĻŦāĻ function/operaton = diameter/area;
āĻ§āĻžāĻĒ ā§ĒāĻ callback function āĻā§ function āĻāĻŋāĻ° āĻāĻŋāĻ¤āĻ° call āĻāĻ°āĻŋāĨ¤ argument āĻ āĻĒā§āĻ°ā§ā§āĻāĻ¨ā§ā§ value pass āĻāĻ°āĻŋāĨ¤
āĻāĻāĻ¨ function/operation āĻāĻŋ āĻ āĻā§āĻŽā§āĻāĻŋāĻ āĻĒā§āĻ°āĻžāĻĒā§āĻ¤ value āĻĨā§āĻā§ operation āĻ¸āĻŽā§āĻĒāĻ¨ā§āĻ¨ āĻāĻ°ā§ HoFs āĻāĻ° āĻ¯ā§āĻāĻžāĻ¨ā§ call āĻāĻ°āĻž āĻšā§ā§āĻā§, āĻ¸ā§āĻāĻžāĻ¨ā§ āĻĒā§āĻ°āĻĻāĻžāĻ¨ āĻāĻ°āĻŦā§āĨ¤
đ ā§¨. āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻšāĻŋāĻ¸ā§āĻŦā§ function āĻĒā§āĻ°āĻĻāĻžāĻ¨ āĻāĻ°ā§(return a function):
Higher Order Function āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻšāĻŋāĻ¸ā§āĻŦā§ āĻ
āĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ function āĻāĻ° return āĻĢāĻ˛ āĻā§āĻ°āĻšāĻŖ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤
function higherOrderFunction (a, b) {
function sum (b) {
return a + b;
}
return sum;
}
console.log( higherOrderFunction(4)(5));
// Output: 9
// Or
function higherOrderFunction (a, b) {
return function sum (b) {
return a + b;
}
}
console.log( higherOrderFunction(4)(5));
// Output: 9
āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°āĻ
- Array: map(), reduce(), filter(), sort()...
- Object: Object.entries()
- Custom
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§§āĻ
āĻāĻāĻāĻŋ array āĻāĻ° āĻĒā§āĻ°āĻ¤āĻŋāĻāĻŋ number element āĻā§ ā§¨ āĻĻā§āĻŦāĻžāĻ°āĻž āĻā§āĻŖ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
const users = [
{firstName: 'Khan', lastName: 'Rabiul', age: 30},
{firstName: 'Anisul', lastName: 'Islam', age: 20},
{firstName: 'Shahidul', lastName: 'Islam', age: 25},
{firstName: 'Mr.', lastName: 'Sabbir', age: 32},
{firstName: 'Sk.', lastName: 'Shamim', age: 37},
]
const usersFullName = users.map(user => user.firstName + ' ' + user.lastName);
console.log(usersFullName);
// Output: ['Khan Rabiul', 'Anisul Islam', 'Shahidul Islam', 'Mr. Sabbir', 'Sk. Shamim']
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§ŠāĻ
āĻāĻāĻāĻŋ array of object āĻĨā§āĻā§ age āĻāĻ° āĻ¸āĻŽāĻˇā§āĻāĻŋ āĻŦā§āĻ° āĻāĻ°āĻ¤ā§ āĻšāĻŦā§;
const users = [
{firstName: 'Khan', lastName: 'Rabiul', age: 30},
{firstName: 'Anisul', lastName: 'Islam', age: 20},
{firstName: 'Shahidul', lastName: 'Islam', age: 25},
{firstName: 'Mr.', lastName: 'Sabbir', age: 32},
{firstName: 'Sk.', lastName: 'Shamim', age: 37},
]
const ageOver30 = users.filter(user => user.age > 30);
console.log(ageOver30);
//Output : {firstName: 'Mr.', lastName: 'Sabbir', age: 32},
// {firstName: 'Sk.', lastName: 'Shamim', age: 37},
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§ĢāĻ
āĻāĻāĻāĻŋ array of object āĻĨā§āĻā§ āĻ¯āĻžāĻĻā§āĻ° age āĻāĻ° āĻā§āĻ°āĻŽāĻžāĻ¨ā§āĻ¸āĻžāĻ°ā§ āĻ˛āĻŋāĻ¸ā§āĻ āĻŦā§āĻ° āĻāĻ°āĻ¤ā§ āĻšāĻŦā§;
const users = [
{firstName: 'Khan', lastName: 'Rabiul', age: 30},
{firstName: 'Anisul', lastName: 'Islam', age: 20},
{firstName: 'Shahidul', lastName: 'Islam', age: 25},
{firstName: 'Mr.', lastName: 'Sabbir', age: 32},
{firstName: 'Sk.', lastName: 'Shamim', age: 37},
]
const sortedByAge = users.sort((a, b) => a.age - b.age);
console.log(sortedByAge);
// Output:
// {firstName: 'Anisul', lastName: 'Islam', age: 20}
// {firstName: 'Shahidul', lastName: 'Islam', age: 25}
// {firstName: 'Khan', lastName: 'Rabiul', age: 30}
// {firstName: 'Mr.', lastName: 'Sabbir', age: 32}
// {firstName: 'Sk.', lastName: 'Shamim', age: 37}
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§ŦāĻ āĻāĻĒāĻ°ā§āĻ° āĻāĻĻāĻžāĻšāĻ°āĻŖ āĻā§āĻ˛ā§ āĻ¯āĻĻāĻŋāĻ āĻāĻŽāĻ°āĻž HoFs āĻāĻ° āĻ¸āĻžāĻšāĻžāĻ¯ā§āĻ¯ā§ āĻ¸āĻŽāĻžāĻ§āĻžāĻ¨ āĻāĻ°ā§āĻāĻŋāĨ¤ āĻāĻāĻžāĻ¨ā§ āĻāĻāĻāĻŋ āĻŦāĻŋāĻˇā§ āĻ˛āĻā§āĻˇāĻŖā§ā§ āĻ¯ā§, āĻĒā§āĻ°āĻ¤āĻŋ āĻā§āĻˇā§āĻ¤ā§āĻ°ā§ āĻāĻŽāĻžāĻ°āĻž āĻāĻāĻ array of object input āĻ āĻā§āĻ°āĻšāĻŖ āĻāĻ°āĻāĻŋ āĻāĻ° āĻāĻŋāĻ¨ā§āĻ¨ operation āĻāĻžāĻ˛āĻžāĻā§āĻāĻŋāĨ¤ āĻ¯āĻĻāĻŋ āĻāĻŽāĻ°āĻž āĻāĻāĻāĻŋ function create āĻāĻ°āĻŋ, āĻ¯ā§āĻāĻžāĻ¨ā§ āĻāĻāĻāĻŋ input āĻāĻŦāĻ āĻāĻŋāĻ¨ā§āĻ¨ operation āĻāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻŋāĻ¨ā§āĻ¨ function callback āĻ āĻāĻ¨āĻĒā§āĻ āĻ¨āĻŋāĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§āĨ¤ āĻ¤āĻž āĻšāĻ˛ā§ function āĻāĻŋ āĻĒā§āĻ¨āĻ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ¯ā§āĻā§āĻ¯ āĻ āĻāĻ°ā§ āĻĄāĻžāĻāĻ¨āĻžāĻŽāĻŋāĻ āĻšāĻŦā§āĨ¤
const users = [
{firstName: 'Khan', lastName: 'Rabiul', age: 30},
{firstName: 'Anisul', lastName: 'Islam', age: 20},
{firstName: 'Shahidul', lastName: 'Islam', age: 25},
{firstName: 'Mr.', lastName: 'Sabbir', age: 32},
{firstName: 'Sk.', lastName: 'Shamim', age: 37},
];
// āĻāĻŽāĻžāĻĻā§āĻ° āĻĒā§āĻ°ā§ā§āĻāĻ¨ā§ āĻāĻŋāĻ¨ā§āĻ¨ function āĻ¸āĻŽā§āĻšāĻ
// ā§Ļā§§, āĻāĻāĻāĻŋ array of object āĻĨā§āĻā§ users full name āĻ˛āĻŋāĻ¸ā§āĻ āĻ¤ā§āĻ°ā§ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§;
const getFullName = function(user) {
return user.firstName + " " + user.lastName
}
// āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§¨, āĻāĻāĻāĻŋ array of object āĻĨā§āĻā§ āĻ¯āĻžāĻĻā§āĻ° age ā§Šā§Ļ āĻāĻ° āĻŦā§āĻļāĻŋ āĻ¤āĻžāĻĻā§āĻ° āĻ˛āĻŋāĻ¸ā§āĻ āĻŦā§āĻ° āĻāĻ°āĻ¤ā§ āĻšāĻŦā§;
const getAgeOver30 = user => user.age < 30 ? user : null;
const calculate = function (users, callbackFunc) {
let output = []; // Output store āĻšāĻŦā§
// āĻ¯ā§āĻšā§āĻ¤ā§ input āĻāĻāĻāĻŋ array āĻāĻŦāĻ āĻāĻ° āĻĒā§āĻ°āĻ¤āĻŋāĻāĻŋ input āĻāĻ° āĻāĻĒāĻ° function āĻāĻžāĻ āĻāĻ°āĻŦā§ āĻ¤āĻžāĻ for loop
for(let i = 0; i <users.length; i++) {
// output.push(callbackFunc(users[i]));
// To handel null and undefined valu
const result = callbackFunc(users[i]);
if (result != null) {
output.push(result);
}
}
return output;
}
console.log(calculate(users, getAgeOver30));
// Output:
// {firstName: 'Anisul', lastName: 'Islam', age: 20}
// {firstName: 'Shahidul', lastName: 'Islam', age: 25}
console.log(calculate(users, getFullName));
// Output: ['Khan Rabiul', 'Anisul Islam', 'Shahidul Islam', 'Mr. Sabbir', 'Sk. Shamim']
const numbers = [4,5,8,3,7,9,10,56];
const calculate = function(numbers, operation) {
let output = [];
for(let i = 0; i < numbers.length; i++) {
if (output !== null) {
output.push(operation(numbers[i]));
}
}
return output;
}
// Double
const double = function (number) {
return number * 2;
}
// Triple
const triple = function (number) {
return number * 3;
}
// power
const power4 = function (number) {
return Math.pow(number,4);
}
console.log(calculate(numbers, double));
// Output: [8, 10, 16, 6, 14, 18, 20, 112]
console.log(calculate(numbers, triple));
// Output: [12, 15, 24, 9, 21, 27, 30, 168]
console.log(calculate(numbers, power4));
// Output: [256, 625, 4096, 81, 2401, 6561, 10000, 9834496]
āĻ āĻ°ā§āĻĨāĻžā§ function return āĻĒā§āĻ˛ā§ āĻŦāĻž arguments āĻĨā§āĻā§ āĻāĻ āĻŦāĻž āĻāĻāĻžāĻ§āĻŋāĻ function āĻā§āĻ°āĻšāĻŖ āĻāĻ°āĻ˛ā§ function āĻāĻŋ, Higher Order functionāĨ¤
đ§âđģ Recursion Function
āĻ¯āĻāĻ¨ āĻā§āĻ¨ āĻ¸āĻŽāĻ¸ā§āĻ¯āĻž āĻ¸āĻŽāĻžāĻ§āĻžāĻ¨ā§āĻ° āĻāĻ¨ā§āĻ¯ āĻāĻāĻŋ āĻāĻžāĻ āĻŦāĻžāĻ° āĻŦāĻžāĻ° āĻāĻ°āĻ¤ā§ āĻšā§, āĻ¤āĻāĻ¨ āĻ¤āĻžāĻā§ Recursive Function āĻŦāĻ˛ā§āĨ¤
āĻĒā§āĻ°āĻ¤ā§āĻ¯āĻžāĻļāĻŋāĻ¤ āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻĒāĻžāĻā§āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻā§āĻ¨ āĻāĻžāĻ āĻŦāĻžāĻ° āĻŦāĻžāĻ° āĻāĻ°āĻž(function call āĻāĻ°āĻž)-āĻā§ recursion function āĻŦāĻ˛ā§āĨ¤ **
āĻāĻĻāĻžāĻšāĻ°āĻŖ: āĻĢā§āĻ¯āĻžāĻā§āĻā§āĻ°āĻŋāĻ¯āĻŧāĻžāĻ˛ āĻĢāĻžāĻāĻļāĻ¨
function factorial (num) {
// base case
if(num === 0) {
return 1;
}
return num * factorial(num -1);
}
console.log(factorial(5));
// Output: 120
đ§âđģ Recursion āĻā§āĻāĻžāĻŦā§ āĻāĻžāĻ āĻāĻ°ā§āĻ
- Function Declaration
- Base Case
- Recursive Call command
đ Function Declaration:
āĻ¸āĻžāĻ§āĻžāĻ°āĻŖ function āĻ¯ā§āĻāĻžāĻŦā§ declare āĻāĻ°āĻž āĻšā§, āĻāĻāĻžāĻ āĻ āĻŋāĻ āĻ¤ā§āĻŽāĻ¨āĨ¤
function recursionFunction () {
}
Base Case:
Base Case āĻ *recursion function*
āĻāĻ° āĻŽā§āĻ˛ āĻāĻŋāĻ¤ā§āĻ¤āĻŋāĨ¤
Base case āĻāĻžāĻĄāĻŧāĻž *recursion function*
āĻāĻāĻāĻŋ āĻ
āĻ¸ā§āĻŽ āĻ˛ā§āĻĒā§ āĻĒāĻ°āĻŋāĻŖāĻ¤ āĻšāĻŦā§ āĻāĻŦāĻ āĻĒā§āĻ°ā§āĻā§āĻ°āĻžāĻŽ āĻā§āĻ°ā§āĻ¯āĻžāĻļ āĻāĻ°āĻŦā§āĨ¤
*Recursion*
āĻĢāĻžāĻāĻļāĻ¨ā§ "base case" āĻšāĻ˛ā§ āĻāĻŽāĻ¨ āĻāĻāĻāĻŋ āĻļāĻ°ā§āĻ¤ āĻ¯āĻž self-calling āĻŦāĻ¨ā§āĻ§ āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻ¯āĻŧāĨ¤ āĻāĻ āĻļāĻ°ā§āĻ¤āĻāĻŋ āĻĒā§āĻ°āĻŖ āĻšāĻ˛ā§ āĻĢāĻžāĻāĻļāĻ¨āĻāĻŋ āĻāĻ° āĻ¨āĻŋāĻā§āĻā§ āĻāĻ˛ āĻāĻ°ā§ āĻ¨āĻž āĻāĻŦāĻ āĻāĻāĻāĻŋ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻŽāĻžāĻ¨ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°ā§āĨ¤
`*Base case*` āĻŽā§āĻ˛āĻ¤ āĻāĻāĻāĻŋ āĻ¸ā§āĻāĻĒāĻŋāĻ āĻĒāĻ¯āĻŧā§āĻ¨ā§āĻ, āĻ¯āĻž āĻ°āĻŋāĻāĻžāĻ°āĻ¸āĻ¨āĻā§ āĻ āĻ¸ā§āĻŽ āĻ˛ā§āĻĒā§ āĻĒāĻ°āĻŋāĻŖāĻ¤ āĻšāĻāĻ¯āĻŧāĻž āĻĨā§āĻā§ āĻ°āĻā§āĻˇāĻž āĻāĻ°ā§āĨ¤
āĻāĻĒāĻ°ā§āĻ° āĻāĻĻāĻžāĻšā§āĻ°āĻ¨ā§āĻāĻŋāĻ¤ā§ *base case
* āĻšāĻŋāĻ¸ā§āĻŦā§ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšā§ā§āĻā§āĨ¤ āĻāĻāĻžāĻ¨ā§ āĻ¯āĻāĻ¨ num = 0; āĻšāĻŦā§ *return
* value āĻšāĻŦā§ 1 āĻāĻŦāĻ *function*
āĻāĻŋ āĻŦāĻ¨ā§āĻ§ āĻšāĻŦā§āĨ¤ **
if(num === 0) {
return 1;
}
đ The Recursion Call command:
āĻāĻ āĻ
āĻāĻļāĻāĻŋ āĻŽā§āĻ˛āĻ¤ āĻāĻāĻāĻŋ funciton
āĻŦāĻžāĻ° āĻŦāĻžāĻ° *call*
āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻĻāĻžā§ā§āĨ¤ āĻāĻŦāĻžāĻ° āĻ
āĻāĻļ āĻĨā§āĻā§āĻ āĻāĻžāĻā§āĻā§āĻˇāĻŋāĻ¤ āĻĢāĻ˛āĻžāĻĢāĻ˛ āĻ¨āĻŋāĻ°ā§āĻ§āĻžāĻ°āĻŋāĻ¤ āĻšā§āĨ¤ āĻāĻĻāĻžāĻšāĻ°āĻŖ āĻāĻ°āĻ
*return num * factorial(num -1);*
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§¨āĻ āĻāĻāĻāĻŋ function āĻ¤ā§āĻ°ā§ āĻāĻ°, āĻ¯ā§āĻāĻŋ āĻĒā§āĻ°āĻžāĻĒā§āĻ¤ āĻ¸āĻāĻā§āĻ¯āĻžāĻā§ āĻŦāĻŋāĻĒāĻ°ā§āĻ¤āĻā§āĻ°āĻŽā§ āĻāĻāĻāĻĒā§āĻ āĻĒā§āĻ°āĻĻāĻžāĻ¨ āĻāĻ°āĻŦā§;
// function declaration
function decendingOrder (num) {
let decndingNumbers = [];
// base case
if(num <= 0) {
return;
}
console.log(num);
decendingOrder(num - 1);
}
console.log( decendingOrder(5))
// Output: 5 4 3 2 1
āĻāĻĻāĻžāĻšāĻ°āĻŖ ā§ŠāĻ āĻāĻāĻāĻŋ function āĻ¤ā§āĻ°ā§ āĻāĻ°, āĻ¯ā§āĻāĻŋ āĻĒā§āĻ°āĻžāĻĒā§āĻ¤ stirng āĻŦāĻŋāĻĒāĻ°ā§āĻ¤āĻā§āĻ°āĻŽā§ āĻāĻāĻāĻĒā§āĻ āĻĒā§āĻ°āĻĻāĻžāĻ¨ āĻāĻ°āĻŦā§;
function reverseString (string) {
if (string.length == 1) {
return string;
} else {
return string.slice(-1) + reverseString(string.slice(0, -1));
}
}
console.log(reverseString("string"));
//Output: "gnirts"
// 1. slice āĻŽā§āĻĨāĻĄāĻāĻŋ āĻ¸ā§āĻā§āĻ°āĻŋāĻ āĻĨā§āĻā§ āĻāĻāĻāĻŋ āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻ āĻ
āĻāĻļ āĻāĻžāĻā§ āĻāĻŦāĻ āĻ¨āĻ¤ā§āĻ¨ āĻāĻāĻāĻŋ āĻ¸ā§āĻā§āĻ°āĻŋāĻ āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻāĻ°ā§āĨ¤
// 2. āĻāĻāĻžāĻ¨ā§ slice(-1) āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°āĻž āĻšā§ā§āĻā§, āĻ¯āĻžāĻ° āĻŽāĻžāĻ¨ā§ āĻšāĻ˛ā§ āĻ¸ā§āĻā§āĻ°āĻŋāĻ āĻāĻ° āĻļā§āĻˇ āĻ
āĻā§āĻˇāĻ°āĻāĻŋ āĻā§āĻā§ āĻ¨ā§āĻā§āĻžāĨ¤
// āĻāĻĻāĻžāĻšāĻ°āĻŖ: "string".slice(-1) āĻāĻ° āĻāĻāĻāĻĒā§āĻ āĻšāĻŦā§ "g"āĨ¤
// string.slice(0, -1):
// slice(0, -1) āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ°ā§ āĻ¸ā§āĻā§āĻ°āĻŋāĻ āĻāĻ° āĻĒā§āĻ°āĻĨāĻŽ āĻĨā§āĻā§ (āĻāĻ¨ā§āĻĄā§āĻā§āĻ¸ 0 āĻĨā§āĻā§) āĻļā§āĻˇā§āĻ° āĻ āĻŋāĻ āĻāĻā§āĻ° āĻ
āĻā§āĻˇāĻ° āĻĒāĻ°ā§āĻ¯āĻ¨ā§āĻ¤ āĻ¸āĻŦāĻāĻŋāĻā§ āĻā§āĻā§ āĻ¨ā§āĻā§āĻž āĻšā§āĨ¤
// āĻāĻĻāĻžāĻšāĻ°āĻŖ: "string".slice(0, -1) āĻāĻ° āĻāĻāĻāĻĒā§āĻ āĻšāĻŦā§ "strin"āĨ¤
// + āĻ
āĻĒāĻžāĻ°ā§āĻāĻ°:
// + āĻ
āĻĒāĻžāĻ°ā§āĻāĻ° āĻāĻāĻžāĻ¨ā§ āĻ¸ā§āĻā§āĻ°āĻŋāĻ āĻāĻ¨āĻā§āĻ¯āĻžāĻāĻŋāĻ¨ā§āĻļāĻ¨ (āĻĻā§āĻ āĻŦāĻž āĻ¤āĻ¤ā§āĻ§āĻŋāĻ āĻ¸ā§āĻā§āĻ°āĻŋāĻ āĻāĻāĻ¤ā§āĻ°āĻŋāĻ¤ āĻāĻ°āĻž) āĻāĻ° āĻāĻ¨ā§āĻ¯ āĻŦā§āĻ¯āĻŦāĻšā§āĻ¤ āĻšāĻā§āĻā§āĨ¤
// āĻāĻĻāĻžāĻšāĻ°āĻŖ: "g" + "strin" āĻāĻ° āĻāĻāĻāĻĒā§āĻ āĻšāĻŦā§ "gstrin"āĨ¤
// reverseString("string") āĻāĻ° āĻ§āĻžāĻĒā§ āĻ§āĻžāĻĒā§ āĻĒā§āĻ°āĻ¸ā§āĻ¸āĻŋāĻ āĻšāĻŦā§:
// āĻĒā§āĻ°āĻĨāĻŽ āĻāĻ˛: "g" + reverseString("strin")
// āĻĻā§āĻŦāĻŋāĻ¤ā§ā§ āĻāĻ˛: "n" + reverseString("stri")
// āĻ¤ā§āĻ¤ā§ā§ āĻāĻ˛: "i" + reverseString("str")
// āĻāĻ¤ā§āĻ°ā§āĻĨ āĻāĻ˛: "r" + reverseString("st")
// āĻĒāĻā§āĻāĻŽ āĻāĻ˛: "t" + reverseString("s")
// āĻˇāĻˇā§āĻ āĻāĻ˛ (Base case): "s"
//āĻĢāĻžāĻāĻ¨āĻžāĻ˛āĻŋ, āĻ¸āĻŦ āĻāĻ¨āĻā§āĻ¯āĻžāĻāĻŋāĻ¨ā§āĻļāĻ¨ āĻšā§ā§ āĻāĻ˛ā§āĻā§ āĻ¸ā§āĻā§āĻ°āĻŋāĻ "gnirts" āĻ°āĻŋāĻāĻžāĻ°ā§āĻ¨ āĻšāĻŦā§āĨ¤
đ Or, with loop
const str = "small";
function rev(str) {
let revStr= "";
for(let i = str.length -1; i>= 0; i--) {
revStr += str[i];
}
return revStr;
}
console.log(rev(str));
// Output: llams
Recursion function āĻā§āĻĄ āĻĒā§āĻž āĻ āĻ¸āĻšāĻā§ āĻā§āĻ˛ āĻā§āĻā§ āĻĒā§āĻ¤ā§ āĻ¸āĻžāĻšāĻžāĻ¯ā§āĻ¯ āĻāĻ°ā§ āĻāĻŋāĻ¨ā§āĻ¤ā§ āĻ āĻ¸āĻāĻā§āĻ¯āĻŦāĻžāĻ° function call āĻāĻ°āĻžāĻ° āĻāĻžāĻ°āĻŖā§ performance āĻāĻžāĻ°āĻžāĻĒ āĻšāĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤
đ§âđģ Currying
currying function āĻāĻŽāĻ¨ functional āĻ§āĻžāĻ°āĻŖāĻž āĻ¯ā§āĻāĻžāĻ¨ā§ āĻāĻāĻžāĻ§āĻŋāĻ arguments āĻĨāĻžāĻāĻ˛ā§āĻ function āĻāĻŋ āĻāĻ āĻ¸āĻžāĻĨā§ āĻāĻāĻāĻŋāĻ° āĻŦā§āĻļāĻŋ argument receive āĻāĻ°ā§ āĻ¨āĻžāĨ¤ āĻ
āĻ°ā§āĻĨāĻžā§ āĻĒā§āĻ°āĻ¤āĻŋāĻāĻŋ argument āĻāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ function declare āĻ return āĻāĻ°ā§āĨ¤ āĻ¤āĻŦā§ āĻā§āĻ¨ function āĻāĻ° āĻāĻ¨ā§āĻ¯ argument pass āĻāĻ°āĻ˛ā§āĻ function āĻāĻŋ āĻ¸āĻ āĻŋāĻāĻāĻžāĻŦā§ āĻāĻžāĻ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°ā§āĨ¤ āĻāĻāĻŋ āĻā§āĻĄā§āĻ° āĻĒā§āĻ¨āĻāĻŦā§āĻ¯āĻŦāĻšāĻžāĻ°āĻ¯ā§āĻā§āĻ¯āĻ¤āĻž āĻŦāĻžāĻĄāĻŧāĻžāĻ¯āĻŧ āĻāĻŦāĻ āĻā§āĻĄāĻā§ āĻā§āĻ āĻ āĻ¸ā§āĻĒāĻˇā§āĻ āĻāĻ°ā§ āĻ¤ā§āĻ˛ā§āĨ¤
function add(a) {
return function(b) {
return function(c) {
return a + b + c;
}
}
}
console.log(add(1)(2)(3)); // Output: 6
*āĻāĻāĻāĻŋ argument āĻ¨āĻž āĻĻāĻŋā§ā§āĻ *
function add(a) {
return function(b) {
return function(c) {
return a + b ;
}
}
}
console.log(add(1)(2)()); // Output: 3
Posted on September 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.