Reputation: 2277
This is my only JavaScript code.
(function (){
var name = "Joby";
console.log(name);
})();
console.log(name);
The console.log inside the function block is printing "Joby" as expected.
The outer console is printing in console. I am expecting "Reference error" for outer console statement. Why is it like that?
I am using Google Chrome browser. Same output is shown in firefox.
Upvotes: 1
Views: 39
Reputation: 36599
name
is a key/property
of window
object, name(global variable)
variable referred out of the IIFE
refers to name
in window
Window.name
, Gets/sets the name of thewindow
(function() {
var name = "Joby";
console.log(name);
})();
console.log(name);
console.log('---With some other variable name---');
(function() {
var name1 = "Joby";
console.log(name1);
})();
console.log(name1);//you can find `ReferenceError` here!
Upvotes: 6