In JavaScript, functions are objects and treated as First-Class Citizens.
First-Class means the language treats a function exactly like any other variable (Number, String, Object).
What makes a Function Object different from a regular {} Object? It has three hidden internal properties defined by the engine.
[[Call]]: The code logic. This slot is triggered when we invoke the function with ().[[Construct]]: The factory logic. This slot is triggered only when we invoke the function with the new keyword (creates a new instance).[[Environment]]: A pointer to the lexical Environment record where the function was created. In case of closures, [[Environment]] is the specific pointer that links that function to the Context Object in the Heap.ES6 introduced Arrow Functions not just as shorter syntax, but as a simpler architectural alternative.
Arrow functions are designed to be non constructible.
[[Construct]] slot.new with an Arrow Function. If we try new Arrow(), the engine throws a TypeError because the internal factory logic simply doesn't exist.this context determined by how they are called.this. They resolve this lexically (they look up to the [[Environment]] of their parent).When a function is executed, it receives inputs. How the engine handles these inputs has evolved.
Historically, every standard function automatically created a hidden object called arguments in the Heap.
0, 1 and .length) but is not an Array. It is an Array like Object..map() or .filter() on it.arguments object....args)ES6 introduced the Rest Operator to handle variable-length inputs architecturally correctly.
function demo(...args) { }A Higher Order Function is defined by the capabilities of First-Class Citizens. It is any function that:
map, filter, reduce)These methods represent the Functional Programming approach. They rely on Immutability (creating new arrays in the Heap rather than changing the old one).
(item, index) => newItem(item, index) => Booleantrue keeps the item; false discards it.(accumulator, currentItem, currentIndex, sourceArray) => nextAccumulator;.reduce(fn, initialValue).
accumulator starts as initialValue.accumulator takes the first element arr[0].forEach)This method represents the Imperative approach.
(item, index) => voidundefined. It exists solely to execute a function for every item (Side Effects).| Feature | Standard Function function | Arrow Function => |
|---|---|---|
| Internal Slots | [[Call]], [[Construct]], [[Environment]] | [[Call]], [[Environment]] |
| Constructible | Yes (can use new) | No (TypeError) |
| Arguments | Has arguments object | No arguments object |
| Scope | Hoisted | Not Hoisted (Variable rules) |
| HOF Strategy | Return Value | Purpose |
|---|---|---|
map | New Array | Transformation (A to B) |
filter | New Array | Selection (Subset) |
reduce | Single Value | Aggregation (Sum/Total) |
forEach | undefined | Side Effects (Action) |