2 ms·
I agree with the way you've responded. But there can be a case where omitting var on a variable definition will not assign it on the global object. That is, if
by sopooneo 10y ago
I agree with the way you've responded. But there can be a case where omitting var on a variable definition will not assign it on the global object. That is, if you hit a variable by the same name as you go up the scope stack, you will just overwrite that variable's value. Example:
function f1(){
var v = 'val';
var f2 = function(){
v = 'inner'; // will not hit global scope
}
f2();
console.log(v);
}
f1();
console.log(v);
output:
val
Uncaught ReferenceError: v is not defined