0

What the difference between Creation Phase and Execution phase in hoisted variables and functions. also how Execution Context is working at this case in javascript console

Refer to this example :

b();
console.log(a);

var a = 'Hello World';

function b() {
    console.log('Called b!');
}

Output:

Called b!
undefined

How can you explain this scenario:

in function case it returns Called b!

in variable case it returns undefined

Oghli
  • 2,200
  • 1
  • 15
  • 37
  • 1
    It's not clear what is not clear in that code. `Called b!` is printed because you printed exactly that. `undefined` is printed because the `a` variable is not initialised at that point. – zerkms Nov 11 '17 at 09:02
  • 1
    https://tc39.github.io/ecma262/#sec-functiondeclarationinstantiation --- here is where the function declaration runtime semantics is explained. – zerkms Nov 11 '17 at 09:05
  • @zerkms why at function case it reruns called b! even that i declared my function after calling it and in variable case it return undefined when the variable is actually not created yet. how execution context is working in runtime? – Oghli Nov 11 '17 at 09:07
  • Check the link to the standard, it explains how the function declaration is handled in runtime. – zerkms Nov 11 '17 at 09:07
  • also why it doesn't return `Uncaught ReferenceError: a is not defined` when printing a. – Oghli Nov 11 '17 at 09:12
  • Because `a` is declared (it's just not initialised). – zerkms Nov 11 '17 at 09:14
  • yes it declared but after `console.log(a)` statement if we Suppose execution context works line by line it shouldn't print `undefined` actually it should return runtime error. – Oghli Nov 11 '17 at 09:17
  • 1
    Given you know the "hoisting" term, is it safe to assume you checked something about what it means? I am asking because it was explained million times in details, and there is really no reason to do it again, unless you have any particular problem. – zerkms Nov 11 '17 at 09:18
  • "yes it declared but after" --- it's hoisted, so the declaration is made before any other statement is executed. – zerkms Nov 11 '17 at 09:20
  • you are right it's not safe to assume that, but I asked because I am trying to understand how it's actually working in execution context. – Oghli Nov 11 '17 at 09:22
  • `var a` declaration is made before any other statement in your code. By the moment it reaches the second line - it's declared but uninitialised. – zerkms Nov 11 '17 at 09:25
  • in this case when javascript console executed these lines of code first it will setup location in memory for variable `a` but it will not set the assigned value until it reaches this line of code `var a = 'Hello World';` – Oghli Nov 11 '17 at 09:30
  • and because i print it before assignment statement it will be `undefined` – Oghli Nov 11 '17 at 09:34
  • 1
    @Oghli Yes, that's what happens – Bergi Nov 11 '17 at 09:38
  • 2
    I wouldn't speak of that in terms of "location in memory" (since the standard does not have a notion of "memory" at all). But yes, it "sets up" a binding. – zerkms Nov 11 '17 at 09:38
  • @zerkms Thanks it really helped me. can you explain how the function is created and executed in runtime – Oghli Nov 11 '17 at 09:43
  • @Oghli the declaration process is explained in the second link. Execution is scattered over few places. Do you really need to know that in such details? Just read the whole standard then ;-) – zerkms Nov 11 '17 at 10:02

0 Answers0