I’m finding myself by Javascript’s variable scope, could someone explain to me why the first example doesn’t work but the second does?
function test() {
return typeof(the_variable) !== 'undefined' && the_variable;
}
(function () {
var the_variable = "doesn't work";
console.log(test());
}());
var the_variable = "does work";
console.log(test());
Output I get in my log:
false
does work
Also I would like to know how to do something similar to the first example.
Explained in comments: