I put a global variable within numPrinter function in Javascript.
but if I don't put numPrinter(); before putting console.log(i);
it is a global variable.. global..
and also I don't understand how global variable works after numPrinter()
there's no return i; within numPrinter();
var numPrinter = function(){
i = 30;
};
console.log(i); // ReferenceError: i is not defined
numPrinter();
console.log(i); // 30
ianywhere (withvarorlet), so when you just sayi = 30insidenumPrinter, this "magically" declaresias a global variable and sets it to 30. (This is a strange quirk of Javascript that usually just causes problems, you can avoid it by using strict mode.) So the first time you logi, the variable isn't declared in any accessible scope, hence theReferenceError- thennumPrinter()"magically" creates the globaliand sets it to 30.numPrinter()can create the global i