JavaScript : Functional Programming (English/Hindi)
Tags: webdev, javascript, beginners, programming
Published: 2023-10-03 13:20:00

## English

Functional programming (FP) is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. In JavaScript, you can practice functional programming by using functions as first-class citizens and employing concepts like pure functions, immutability, and higher-order functions.

Here's an example of functional programming in JavaScript:

**Using Pure Functions:**

```javascript
// Pure function: It always produces the same output for the same input
function add(a, b) {
  return a + b;
}

const result = add(3, 5);
console.log(result); // 8
```

In this example, the **add** function is a pure function because it always produces the same output for the same input. It doesn't modify any external state or variables.

**Immutability:**

```javascript
// Using immutability
const numbers = [1, 2, 3, 4, 5];

// Instead of modifying the original array, create a new one
const doubledNumbers = numbers.map((num) => num * 2);

console.log(doubledNumbers); // [2, 4, 6, 8, 10]
```

Here, we're using the **map** function to double each number in the **numbers** array without modifying the original array. This follows the principle of immutability.

**Higher-Order Functions:**

```javascript
// Higher-order function: It takes a function as an argument
function operateOnArray(array, operation) {
  return array.map(operation);
}

const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = operateOnArray(numbers, (num) => num * 2);
console.log(doubledNumbers); // [2, 4, 6, 8, 10]
```

In this example, **operateOnArray** is a higher-order function that takes an array and an operation (a function) as arguments. It then applies the operation to each element of the array.

## Hindi

Functional programming (FP) ek programming paradigm hai jo computation ko ganitik (mathematical) functions ke moolyaankan ke roop mein dekhta hai aur state aur mutable data ko badalne se bachta hai. JavaScript mein aap functional programming ka abhyas kar sakte hain functions ko pehli darja ke nagarik (first-class citizens) ke roop mein istemal karke aur shuddh functions, asamparipata (immutability), aur ucch-ayara functions jaise concepts ka upayog karke.

Neeche ek JavaScript mein functional programming ka udaharan hai:

**Shuddh Functions Ka Upayog:**

```javascript
// Yah hamesha ek hi prakar ke input ke liye ek hi prakar ka output utpann karta hai
function jodna(a, b) {
  return a + b;
}

const parinam = jodna(3, 5);
console.log(parinam); // 8
```

Is udaharan mein, **jodna** function ek shuddh function hai kyun ki yah hamesha ek hi prakar ke input ke liye ek hi prakar ka output utpann karta hai. Ismein kisi bhi bahri sthiti ya variable ko parivartit nahin karta hai.

**Asamparipata (Immutability):**

```javascript
// Asamparipata ka upayog karke
const numbers = [1, 2, 3, 4, 5];

// Moolyaankan (original) array ko parivartit karne ke bajaye, ek naya array banate hain
const dubleNumbers = numbers.map((ank) => ank * 2);

console.log(dubleNumbers); // [2, 4, 6, 8, 10]
```

Yahan, hum **map** function ka upayog karte hain moolyaankan (original) array ke har ank ko duguna karne ke liye, moolyaankan ko parivartit kiye bina. Isse asamparipata ki tatparata ka palan hota hai.

**Ucch-Ayara Functions (Higher-Order Functions):**

```javascript
// Yah ek function ko argument ke roop mein le leta hai
function arrayPar karyaKarna(array, prakriya) {
  return array.map(prakriya);
}

const numbers = [1, 2, 3, 4, 5];

const dubleNumbers = arrayPar karyaKarna(numbers, (ank) => ank * 2);
console.log(dubleNumbers); // [2, 4, 6, 8, 10]
```

Is udaharan mein, **arrayPar karyaKarna** ek ucch-ayara function hai jo ek array aur ek prakriya (function) ko argument ke roop mein leta hai. Phir yah prakriya ko array ke pratyek ank par lagata hai.