---
# System prepended metadata

title: Question - Closure

---

# Question - Closure

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures


Frist snippet
```javascript
function makeFunc() {
  const name = "Mozilla";
  function displayName() {
    console.log(name);
  }
  return displayName;
}

const myFunc = makeFunc();
myFunc();

```

Second snippet

```javascript
function init() {
  var name = "Mozilla"; // name is a local variable created by init
  function displayName() {
    // displayName() is the inner function, that forms the closure
    console.log(name); // use variable declared in the parent function
  }
  displayName();
}
init();

```

1. How does myFunc maintain access to the name variable even after makeFunc has finished executing? Is it because var is functional variables? Or it is because first snipet return the inner function?

2. Why is displayName invoked within init and not returned?


3. Does secod snippet displayName form a closure similarly to the first snippet, even though it's used differently?

