JSFiddle - React, Tailwind, and code Playground
JavaScript
function sayHello(name) {
var text = 'Hello ' + name; // Local variable
console.log(text);
var sayAlert = function () {
alert(text);
}
return sayAlert;
}
sayHello(); // This will write 'Hello undefined' to the console (in Chrome anyway), but will not alert though since it returns a function handle to nothing). Since no handle or reference is created, I imagine a good js engine would destroy/dispose of the internal sayAlert function.
// Create a handle/refernce/instance of sayHello() using the name 'Bob'
sayHelloBob = sayHello('Bob');
sayHelloBob();
// Create another handle or reference to sayHello with a different name
sayHelloGerry = sayHello('Gerry');
sayHelloGerry();
// Now calling them again demonstrates that each handle or reference contains its own unique local variable memory space. They remain in memory 'forever' (or until your computer/browser explode)
sayHelloBob();
sayHelloGerry();