How do JavaScript closures work?

In JavaScript, closure is the grouping of a function and where that function declared. In JavaScript, all functions work like closures. A closure is a function uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.


You can try to run the following code to learn how to work with JavaScript closures:

<!DOCTYPE html>
      <h2>Working with JavaScript Closures</h2>
         var p = 10;
         function a() {
            var p = 15;
            b(function() {
         function b(f){
            var p = 30;