JavaScript Closures: A Simple Guide
Lets tackle and get familiar with JavaScript Closures

Hi there π This is SMY
Curious to know my standout highlights? Let me share some with you:
π― Professional Experience:
β― Full Stack Engineer with ~ 5 years of expertise in the JavaScript ecosystem (Backend / Frontend / AWS). Consistently delivers high-quality SaaS applications by developing, testing, documenting, and maintaining while ensuring code quality and reliability through unit testing.
β― Regularly learning and upskilling myself on various technical and soft skills by participating in initiatives, courses, and developing POCs.
π― Academic Experience:
β― Pursued a Bachelor of Science in Computer Science with a 3.72 CGPA and a four-time Merit Scholarship holder.
β― My academic accomplishments have been further recognized with awards, and I have actively contributed as a volunteer, expanding my horizons beyond traditional educational boundaries.
π― Publications:
β― Passionate about leveraging technology to drive positive change, I am an avid writer and community contributor. I love sharing my insights, experiences, and knowledge through thought-provoking articles and engaging discussions.
β― Discover a collection of my insightful articles on various topics by visiting my profile on hashnode π https://smy.hashnode.dev/
π― Interests:
β― At 4 years old, I was fascinated by computers, starting with gaming on DOS and exploring Pentium III, solidifying my fascination and paving the way for a lifelong journey.
β― Fast forward to today, and I find myself immersed in the ever-evolving landscape of technology. I'm passionate about leveraging technology to solve real-world challenges and make a positive difference in our interconnected world.
π If you're passionate about technology too, or if you're eager to explore the vast opportunities it presents, let's connect π€
LinkedIn: https://www.linkedin.com/in/sm-y/
Hellllooooooo!
Hope you're doing great! This is SMY! Welcome to my second article π
In this article, we are going to learn about Closures in JavaScript.
Contents:
β‘
Closures simple definitonβ‘
Closure Example
Let's start π
Closures are one of the most fundamental topics of JavaScript nowadays. If you happen to give an interview or will, you must know your Closures well!
Closures Simple Definiton
Closures are simply functions bundled together. Each function inside a closure has access to its parents and its parents and so on.. lexical environment or in other words has access to the variables and functions defined, created or initialised in its own scope and its ancestors.
Let's take a look at simple example, in which adding up two numbers.
Example:
const add = (num1) => { // outer function
return (num2) => { // inner function
return num1 + num2;
};
};
console.log(add(1)(2)); // logs 3
Let's break down what's happening on inside.
Whenever a function is created in JavaScript, it makes its own execution context and lexical environment. The inner function(s) have access to its parent or any of its ancestors lexical environment, that is because its own execution context and lexical environment is created inside those.
Lets visualise this with JavaScript.
{ // add execution context and or outer func
const memory = { // outer lexical environment
num1: 1,
};
{ // inner func execution context
const memory = { // inner lexical environment
parentMemory: super.memory
num2: 2,
};
}
}
We can see here how the inner memory has access or reference to all of its parents memory, which contains variables and functions if any.
One thing may be curious to you, is that how can the inner function has access to the num1 if outer function finished executing before inner function?
The reason is, the inner function as visualised above in example code has the reference to parents memory or lexical environment.
That's the basics of how we can use Closures in JavaScript.
That's it, folks! hope it was a good read for you. Thank you! β¨




