console.log(a); var a = 10; console.log(a);
In JavaScript, var declarations are hoisted to the top of their scope and initialized with undefined. So the first console.log(a) prints undefined. Then a is assigned 10, so the second console.log(a) prints 10.
var x = 5; console.log(window.x);
var variables relate to the global object in browsers.In browsers, variables declared with var at the global scope become properties of the global window object. So window.x is 5.
let y = 20; console.log(window.y);
let differs from var in global scope.Unlike var, let declarations at the global scope do NOT create properties on the global window object. So window.y is undefined.
foo(); function foo() { console.log('Hello from foo'); } foo();
Function declarations are hoisted with their body, so foo can be called before its definition. Both calls print 'Hello from foo'.
When the global execution context is created, JavaScript first hoists all variable and function declarations (setting up memory space), then runs the code line by line.