Closure is one of the favourite question to be asked in an frontend interview so how can you answer it perfectly such that interviewer is impressed
Let's understand what is a closure first
In layman terms closure is bundling of 2 functions where inner function has access to methods and properties of outer function even after execution of outer function is done .
another Defination "closure is defined as function bundled with it's lexical scope forms a closure"
function named(name){
return function mixed(){
return "hello"+name
}
}
console.log(named("sagar")())
function x(a) {
function y(b) {
function z(c) {
return a + b + c;
};
return z;
}
return y;
}
// the outer function accepts an argument and returns a function
const a = x(10);
// the inner function also accepts an argument and returns the total of both
// outer and inner argument
let b = a(20);
let c = b(30);
console.log(c);
in this, the inner function has access to the outer function’s arguments and can use it for each instance it is created.
closures are important this can act as my starting point .
Check out my portfolio = sagyr.xyz