I recently came across this javascript code in a competitive site and couldn't understand how this pretty much works.
var a= 1;
(function(){
console.log(a);
var a = 2;
console.log(a);
})();
I expected the output to be..
1 2
But to my surprise, the original output was..
undefined 2
Can someone please explain how it works? Thanks in advance.