Hoisting with closures example

1 · Adrian Matei · Aug. 9, 2021, 10:29 a.m.
Via hoisting, memory is allocated to the variable var1, but it is not initialised by the time the closure is executed function one() { function two() { console.log(`closure var1 - ${var1}`); } three(); var var1 = 'var1'; } one(); //output hoisting var1 - undefined But if we use setTimeout(), by the time callback closure function is executed var1 will have been initialised and its value is printed: function one() { setTimeout(function() { console.log(`closure var1 - ${var1}`);...