function outer() {
const outerConst = 300;
function inner() {
console.log(outerConst)
const innerConst = 500;
}
console.log(outerConst)
inner()
console.log(outerConst)
}
outer()
function outerWrapper() {
let counter = 0;
function incrementCounter() {
counter++
return counter
}
return incrementCounter
}
const counterInc = outerWrapper()
counterInc() // 1
counterInc() // 2
counterInc() // 3
const globalValue = "I'm global"
function outer() {
const outerConst = 300;
console.log(globalValue)
return function inner() {
console.log(outerConst, globalValue)
const innerConst = 500;
}
}
const innerFunc = outer()
innerFunc()
(globalValue)
are also accessible to the inner functionfunction outer(firstName) {
const outerConst = 300;
console.log(firstName, lastName)
// Reference Error: 'lastname' is
// not defined
return function inner(lastName) {
console.log(firstName, lastName)
const innerConst = 500;
}
}
const innerFunc = outer("Yash")
innerFunc("Priyam")
function makeAdder(x) {
const zero = 0;
return function (y) {
return x + y + zero;
};
}
const add5With = makeAdder(5);
const add10With = makeAdder(10);
console.log(add5With(2)); // 7
console.log(add10With(2)); // 12
// global scope
const e = 10;
function sum(a) {
return function sum2(b) {
return function sum3(c) {
// outer functions scope
return function sum4(d) {
// local scope
return a + b + c + d + e;
};
};
};
}
const sum2 = sum(1);
const sum3 = sum2(2);
const sum4 = sum3(3);
const result = sum4(4);
console.log(result); // 20
// global scope
const e = 10;
function sum(a) {
return function (b) {
return function (c) {
// outer functions scope
return function (d) {
// local scope
return a + b + c + d + e;
};
};
};
}
console.log(sum(1)(2)(3)(4)); // 20