The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)

Wanted to share here, the simplest explanation that I have ever seen on the web, for the famous Closures topic. (Credits : roadmap.sh)

A closure is a function that has access to its outer function scope even after the outer function has returned. This…


This content originally appeared on DEV Community and was authored by Keertivaas S

Wanted to share here, the simplest explanation that I have ever seen on the web, for the famous Closures topic. (Credits : roadmap.sh)

A closure is a function that has access to its outer function scope even after the outer function has returned. This means a closure can remember and access variables and arguments of its outer function even after the function has finished.

function outer() {
  const name = 'Roadmap';

  function inner() {
    console.log(name);
  }

  return inner;
}

const closure = outer();
closure(); // Roadmap

In the above example, the inner function has access to the name variable of the outer function even after the outer function has returned. Therefore, the inner function forms a closure.


This content originally appeared on DEV Community and was authored by Keertivaas S


Print Share Comment Cite Upload Translate Updates
APA

Keertivaas S | Sciencx (2024-07-28T13:58:38+00:00) The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh). Retrieved from https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/

MLA
" » The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)." Keertivaas S | Sciencx - Sunday July 28, 2024, https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/
HARVARD
Keertivaas S | Sciencx Sunday July 28, 2024 » The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)., viewed ,<https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/>
VANCOUVER
Keertivaas S | Sciencx - » The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/
CHICAGO
" » The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)." Keertivaas S | Sciencx - Accessed . https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/
IEEE
" » The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh)." Keertivaas S | Sciencx [Online]. Available: https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/. [Accessed: ]
rf:citation
» The simplest explanation I have seen for Closures in JS (Credits : roadmap.sh) | Keertivaas S | Sciencx | https://www.scien.cx/2024/07/28/the-simplest-explanation-i-have-seen-for-closures-in-js-credits-roadmap-sh/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.