Variable hoisting inside IIFE (lazy parsing)
I am getting a very strange output on the below scenarios: function test(){ var test=123; console.log(test) } // this output : 123 (function test(){ var test=123; console.log(test) })() // this output: 123 But when using the below code (function test(){ test=123; console.log(test) })() //output: function test(){ test=123; console.log(test) } Can anybody please explain. What you're seeing isn't related to hoisting. Your first example is quite straightforward: (function test(){ var test=123; console.log(test) })() You're creating a variable (via var ) called test within the scope of the function