0

How is that possible that code below doesn't alert undefined? From my understanding only variables names declaration were hoisted and then on createWorkout() call it's should be undefined but it's not and works perfectly fine.

var start = function() {
  var workout = createWorkout();
  alert(workout.name);
}

var createWorkout = function() {
  var plan = {
    name: "ex1"
  }
  return plan;
}
start();
thefourtheye
  • 206,604
  • 43
  • 412
  • 459
  • 5
    It's because you don't execute `start()` until after declared have created the `createWorkout` function. – synthet1c Dec 03 '16 at 19:17
  • Exact @synthet1c, if you put the `start()` call before the `createWorkout` function, you will get the error: `Uncaught TypeError: createWorkout is not a function(…)` – Alexandre Neukirchen Dec 03 '16 at 19:21
  • hoisting != eval. It pushes up the *declaration* of the *namespace*. It doesn't actually evaluate the expression / assign the value. – Crayon Violent Dec 03 '16 at 19:37

0 Answers0