InterviewSolution
| 1. |
What is a closure and how do we use it? |
|
Answer» Closures are one of the most complex topic in JavaScript, but they are everywhere in JavaScript. Consider the below code. The variable b have an scope in outer function i.e. from LINE 4 to line 10. So, at line 13 when we call the outer function, it can access the value of b, but at line 14 there is no outer function. So, how does the innerFn() access the value of b. This is where the JS feature of Closures comes into play. Output //10 //20 |
|