DEV Community

Heru Hartanto
Heru Hartanto

Posted on

Closure in Javascript

Closure gives you a access to an outer function scope from an inner function.

Here some simple example:

  function init(){
    let firstName = 'John';
    let lastName = 'Snow';
    function greeting(){ // inner function a closure
      return `Hello ${firstName} ${lastName}`;
    }
    return greeting();
  }
  init();
Enter fullscreen mode Exit fullscreen mode

Here another example:

    function myName(){
        let name = 'Herro';
        function displayName(){
            return name;
        }
        return displayName;
    }
    let myFuncName = myName();
    myFuncName();
    // Herro
Enter fullscreen mode Exit fullscreen mode

Let's see what going on. myName() function is run and place inside myFuncName variable and become reference to displayName() function, dispayName() isn't invoke yet.
since displayName() has reference to myName() within which variable name exists, then "Herro" is passed when myFuncName() are invoke.

Top comments (0)