All the declarations in javascript are moved to the top, When the page is executed.
Hoisting Example :
a = 25;
b = a+5/100;
var a;
Here var a; statement will be moved to top by javascript engine.
But this is not true for intializations :
Non Hoisting - intialization Example :
document.write(x);
var x=5;
In this case x will be undefined when the first statement is executed.
Same is true with function declarations also.
var funct1 = function(){
console.log("func1");
}
function funct2(){
console.log("func2");
}
func2 will be hoisted in above case, But func1 will not be hoisted as that is a intialization.
Hoisting Example :
a = 25;
b = a+5/100;
var a;
Here var a; statement will be moved to top by javascript engine.
But this is not true for intializations :
Non Hoisting - intialization Example :
document.write(x);
var x=5;
In this case x will be undefined when the first statement is executed.
Same is true with function declarations also.
var funct1 = function(){
console.log("func1");
}
function funct2(){
console.log("func2");
}
func2 will be hoisted in above case, But func1 will not be hoisted as that is a intialization.
0 comments:
Post a Comment