# function in js

# 1\. 🔑 Functions in JS

In JavaScript:

* **Functions are objects** (special type: `Function` objects).
    
* This is why we can **treat functions like values**.
    

---

# 2\. What are First-Class Functions?

👉 **First-Class Functions** = The ability of functions in JS to be:

1. **Assigned to variables**
    
2. **Passed as arguments** to other functions
    
3. **Returned from other functions**
    
4. **Stored in data structures (arrays, objects, etc.)**
    

> In short → “Functions can be used like any other value.”

---

# 3\. Anonymous Functions

👉 Functions **without a name**.

* Often used when a function is needed only once.
    
* Must be assigned or passed somewhere (otherwise useless).
    

### Example

```javascript
// Anonymous function assigned to a variable
const greet = function() {
    console.log("Hello!");
};

greet(); // Hello!
```

---

# 4\. Function Expressions vs Function Declarations

```javascript
// Function Declaration
function sayHi() {
    console.log("Hi!");
}

// Function Expression (anonymous function stored in a variable)
const sayHello = function() {
    console.log("Hello!");
};
```

### Key difference

* **Declarations** → hoisted fully.
    
* **Expressions** → hoisted but kept in **TDZ** (just like `let`).
    

---

# 5\. Functions as Arguments (Higher-Order Functions)

```javascript
function runTwice(fn) {
    fn();
    fn();
}

runTwice(function() {
    console.log("I was passed as an argument!");
});
```

👉 This works because JS allows passing functions like values.

---

# 6\. Functions as Return Values

```javascript
function multiplier(x) {
    return function(y) {
        return x * y; // closure
    };
}

const double = multiplier(2);
console.log(double(5)); // 10
```

👉 This combines **closures** + **first-class functions**.

---

# 7\. Functions in Data Structures

```javascript
const arr = [
    function() { return "First"; },
    function() { return "Second"; }
];

console.log(arr[0]()); // First
console.log(arr[1]()); // Second

const obj = {
    greet: function() { return "Hello from object!"; }
};

console.log(obj.greet()); // Hello from object!
```

---

# 8\. Higher-Order Functions (HOF)

👉 A function that either:

* **Takes another function as an argument**, or
    
* **Returns a function**
    

Examples: `map`, `filter`, `reduce` in JS are HOFs.

```javascript
const nums = [1, 2, 3, 4];

const doubled = nums.map(function(n) { return n * 2; });
console.log(doubled); // [2, 4, 6, 8]
```

---

# 9\. Arrow Functions (Anonymous Shortcut)

```javascript
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
```

Arrow functions are often **anonymous** and shorthand for function expressions.

---

# 10\. ✅ Takeaways

* JS treats functions as **first-class citizens**.
    
* That’s why **callbacks, promises, async/await, HOFs** work.
    
* Anonymous functions are often used in **callbacks**.
    
* Closures + first-class functions → **functional programming power** in JS.
