Javascript – Hoisting in JavaScript




  
  





Javascript

// Variable Hoisting
var myVariable = 'Outer Value';
var fn = function(){
  alert(myVariable);
  var myVariable = 'New Local Value';
};
fn();
      

// Function Declaration Hoisting
function foo(){
  function bar() {
      return 1;
  }
  return bar();
  function bar() {
      return 2;
  }
}
alert(foo());


// Function Expression Hoisting
function foo2(){
  var bar = function() {
      return 1;
  };
  return bar();
  var bar = function() {
      return 2;
  };
}
alert(foo2());

Share and Enjoy !

0Shares
0 0 0