Search code examples
javascriptfunctional-programmingcomputer-sciencelambda-calculus

Is the substitution model a good way of approximating how JavaScript evaluates pure code?


Say this is my program:

let f = x => x * 2
let g = x => f(x) + 1
let h = x => g(x) > 3
h(2)

Is the substitution model a good mental model for how the JS runtime evaluates this code, or is the way it's actually evaluated very different?

https://en.wikipedia.org/wiki/Lambda_calculus#Substitution https://en.wikipedia.org/wiki/Referential_transparency


Solution

  • The way it is evaluated is quite different, though the results will be the same. The most relevant pure CS model to envision is the environment model. In short, when a statement is evaluated, it may mutate the references that it has at that time. These mutations are managed through scope chains in JS, but this is essentially just another name for the environment above.