Closures are functions using externally declared variables in their own scope. Useful when debugging Closures - console.dir method allows you to preview a function scope.
#javascript
// EXAMPLE 1// Outer scope variable closure// ----------------------varchange=4;functioncalculate(){varbase=50;returnbase+change;// change variable doesn't exist in the calculate function scope. }calculate();// Returns 54// EXAMPLE 2// Inner function closure// ----------------------functionouterFunction(outerVar){functioninnerFunction(innerVar){returnouterVar+' from '+innerVar;}returninnerFunction;}varsaySomething=outerFunction('Hello');// 'Hello' is being passed as the outerVar argument. console.log(saySomething());// Logs 'Hello from undefined' as we haven't set the innerVar yet.console.dir(saySomething);// console.dir() function allows you to preview the current state of the scopesaySomething('closure');// In the second run the argument will passed as the innerVar.