What is hoisting in javascript?
Hoisting is the default behavior of javascript, moving variable on the top. If variable is global then go to the top and if variable in the function then variable go to the top of function. And then initialize. Initialization work will be in same place where we have assigned value.
Also we can define variable (or declare variable) after assigning value.var sheo = "First Name";
console.log(sheo +" "+ sagar);
var sagar = "Last Name";
console.log(sheo +" And "+ sagar);
/* javascript how to work
var sheo;
var sagar;
sheo = "First Name";
console.log(sheo +" And "+ sagar); // Result will be: "First Name And undefined"
sagar = "Last Name";
console.log(sheo +" And "+ sagar); // Result will be: "First Name And Last Name"
*/
// Second methods
ss = "Sheo";
console.log("Second method: " + ss);
var ss;
/* After compilasation
var ss; // declaration part
ss = "First Name"; // initialisation part
console.log("Second method: " + ss);// Result will be: "Sheo"
Because variable declaration work go to the top first then initialization
work in same place where you have initialize.
*/
// Third methods
ss = "Sheo";
console.log("Third method: " + ss); // Result will be: "Sheo"
var ss;
function developer(){
console.log("Third method: " + ss); // Result will be: "undefined"
var ss = "New Sheo Sagar"
console.log("Third method 2: " + ss); // Result will be: "New Sheo Sagar"
}
developer();
/* After compilasation
var ss; // declaration part
ss = "Sheo"; // initialisation part
console.log("Third method: " + ss);// Result will be: "Sheo"
function developer(){
var ss; // declaration part
console.log("Third method: " + ss); // Result will be: "undefined"
ss = "New Sheo Sagar" // initialisation part
console.log("Third method 2: " + ss); // Result will be: "New Sheo Sagar"
}
Because variable declaration work go to the top of the
function first then initialisation work in same place where you have initialize.
*/
No comments:
Note: Only a member of this blog may post a comment.