Curly braces are blocks, like if statements. Variable declarations inside are scoped to the block.
The variable yep is unavailable outside of the if statement. If however it was changed form a const to a var then it would be function scoped and available outside the if statement.
Closures give the ability to access a parent level scope from a child scope, even after the parent level scope has been destroyed.
This example is taken directly from the course:
For a closure to work we must return a function from within a function where the inner functions scope references something from the outer functions scope. Looking at the above example, when we run innerFn() the two console logs are still registered to the console even though the outerVar (as part of the parent/outer function) would normally have been garbage collected having been called during the innerFns creation. This gives us the ability to keep access to variables that would have otherwise been destroyed.
Lets look at another example.
In this example we have an outer function that will be used to create a family. Its scoped variable familyMembers will create a random number from 1 - 9. The inner function will return a string which gives the outer functions parameter as the family name and then uses the outer functions random number as the amount of people in the family. We could go one step further and use the inner function even though the outer function has since been destroyed having been creating via its calling when creating the different families.
More to follow.... 😊