Is it true that every function in JavaScript is a closure?

Is the function created by Function constructor also a closure?

Yes, it closes over the global scope. That might be unintuitive because all other JavaScript closures close over their lexical scope, but it still matches our definition of a closure. In your example, a is a free variable, and resolves to the a in an other scope when the inner/fn function is called somewhere.

If an inner function doesn’t have any free variables, can we still call it a closure?

Depends on whom you ask. Some say Yes, others call them “uninteresting closures”, personally I say No because they don’t reference an outer scope.

Leave a Comment