SocialHelpouts is now CutShort! Read about it here
Who pays how much? Be informed with this salary report on Indian startups.
Why join channels?
Learn from peers
Discuss and share learning resources with the top professionals across the world
Open business or job opportunities
Earn reputation points to get consulting projects, attract talent or land jobs.
Accelerate your growth
Grow your network and get exclusive deals from our learning partners.
signup now
Nipun Soni asked a question

How can you explain JavaScript closure to a beginner?

To a newbie Javascript programmer coming from more traditional programming languages such as C/C++, the idea of closure seems difficult to understand. How would you explain this concept as simply as possible?

submitting answer...
No answers yet. Be the first one to answer!
1 answer
Pratyush I am a JS developer.
Closure can be explained as scope that is still alive when the parent function execution has finished, because it returned another function that might use variables from the parent function's scope later.


function add (a) {
  return function (b) {
    return a + b;

let addFive = add(5),
  addSeven = add(7);

console.log(addFive(10)); //15
console.log(addSeven(9)); //16

So here in addFive(10) ,  the variable a of parent function is alive because it returns a function that uses a.
Loading comments...
To view all answers to this question, join this channel
join this channel
Awesome! You have connected your Facebook account. Like us on Facebook to stay updated.