a JavaScript Closure in one sentence.

In every interview curcit I find myself having to explain what a closure is to people. With it being such a simple thing yet misunderstood I realised today that I should really just have it summed up in one sentence rather than having to bumble through and then in the end just say oh… can I just show you.

A closure is a JavaScript function’s ability to access/remember variables within its local scope after execution.

Hopefully this will stop me looking like a excited teenager eager to learn while staring into my knowledgeable interviewer’s eyes and morph me into the somewhat knowledgeable employable keen JS dev I am.

One thing to watch out for though, is JS by keeping these¬†closures¬†open will not garbage dump inside these functions as they need to keep the variables alive. I haven’t seen this causing me any real problems but performance will be an issue.

A Closure in action