JSFiddle - React, Tailwind, and code Playground
JavaScript
var hello = "Hello";
var world = "World";
// This function takes both variables directly from the
// global scope.
var sayHelloWorld = function(){
alert(hello+" "+world);
}
// Passing arguments to a closure saves a copy of the variables
// at the time they are passed. This prevents a function from
// reading the value of a variable that may have been updated.
var sayHelloWorldClosure = (function(hello_internal){
return function(){
alert(hello_internal+" "+world);
}
})(hello);
hello = "Goodbye";
sayHelloWorld(); // Goodbye World
sayHelloWorldClosure(); // Hello World