𝗙𝗶𝗿𝘀𝘁 𝗢𝗿𝗱𝗲𝗿 𝘃𝘀 𝗙𝗶𝗿𝘀𝘁 𝗖𝗹𝗮𝘀𝘀 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀
People confuse first order and first class functions in JavaScript.
First Order Functions
A first order function is a basic function.
- It does not take another function as an argument.
- It does not return another function.
- It works with numbers or strings.
Example: function multiply(a, b) { return a * b; }
This function takes numbers. It returns a number. No other functions are involved.
First Class Functions
JavaScript supports first class functions. This means functions act like normal values.
You do these things with first class functions:
- Store functions in variables.
- Pass functions as arguments.
- Return functions from other functions.
The Difference
These two terms are not opposites.
- First order describes a type of function.
- First class describes a feature of the language.
Simple View:
- First order = Normal function.
- First class = JS treats functions like variables.