JSFiddle - React, Tailwind, and code Playground
JavaScript
let globalMsg = "I am global message";
function outer() {
console.log(globalMsg);
let outerMsg = "I belong to the scope of outer function.";
function inner() {
let innerMsg = "I only can be accessed by inner";
alert(globalMsg);
alert(outerMsg);
}
alert(innerMsg); // outer cannot access local variable inside inner function
return inner; // Do not add parentheses (), namely, inner();
// they would execute inner function when we invoke outer function
}
// console.log(outerMsg); // error, cannot access variable inside outer function's scope
let callInner = outer();
callInner(); // This invokes inner function wrapped inside the outer function