Functional Programming in JavaScript: Theory, History, and Key Concepts
The article surveys functional programming’s origins, from Hilbert and Church to lambda calculus, explains its pure‑computation model versus imperative state mutation, outlines essential concepts such as currying, higher‑order functions, and immutability, discusses JavaScript‑specific practices and limitations, and notes the growing impact of functional ideas in modern front‑end frameworks.
This article is the theoretical part of a series on writing high‑quality functions in JavaScript. It explains why functional programming exists, its historical background, and the essential concepts that JavaScript developers should know.
Background : The author reviews the evolution of computers and programming languages, introducing key figures such as David Hilbert, Alan Turing, John von Neumann, Alonzo Church, and others. Their work on formal systems and the lambda calculus laid the theoretical foundation for functional languages.
Why functional languages? Functional programming aims to model pure computation – an operation that produces a result without mutating state. The article contrasts this with the von Neumann model, where computation is expressed as modifications of memory.
Lambda calculus : The core formal system is introduced with the syntax λ<variable>.<expression> . Examples include a single‑variable expression λx.x2-2*x+1 and its evaluation (λx.x2-2*x+1)1 = 1-2*1+1 = 0 . A multi‑variable example shows currying: ((λx.λy.2*x+y)1)2 = (λy.2+y) 2 = 4 .
Key questions (10 questions) cover topics such as:
Why functional languages were created and what problems they solve.
The relationship between lambda calculus and Turing machines.
Differences between functional and imperative paradigms.
How recursion replaces loops, how exceptions are handled with functors, and why mutable state is avoided.
JavaScript specific discussion :
Why this is discouraged in functional code because it introduces implicit context.
How JavaScript’s for loops are tolerated to implement array helpers (map, filter, reduce) while keeping the core functions pure.
What it means for functions to be first‑class citizens: they can be stored in variables, passed as arguments, returned from other functions, serialized (e.g., via eval ), and compared.
Example of lazy vs. eager evaluation:
true || console.log('源码终结者')
Non‑lazy example:
let i = 200 console.log(i+=20, i*=2, 'value: ' + i) console.log(i)
Drawbacks of functional programming in JavaScript include the lack of immutable data structures, missing native function composition utilities, no built‑in lazy sequences, and the absence of reliable tail‑call optimization in browsers.
The article also lists many functional‑programming keywords (referential transparency, purity, immutability, higher‑order functions, currying, monads, functors, etc.) and explains each briefly.
Finally, the author reflects on the growing influence of functional ideas in modern front‑end frameworks (React Hooks, Vue 3 Function API) and predicts that JavaScript will increasingly rely on functions to solve most problems.
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.