0

In the below snippet, was f1 accessible from f2, because f1 was in the scope chain for f2?

There are lots of articles that say greet would be accessible from f2 because it is in the scope chain, but how was f1 accessible?

var greet = "hello";
function f1() {return greet;}
function f2() {console.log(f1());}
f2(); // hello

EDIT:

To clarify, I am new to JS; read that when a function is invoked, an Execution Context (EC) is prepared for the function, and the EC has a scope chain (including other things). Was f2 able to access f1 from its EC's scope chain?

Social Developer
  • 405
  • 5
  • 16
  • [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+scope+of+function+declaration) of [javascript scope of function declarations](/q/14665160/4642212). – Sebastian Simon Jun 03 '21 at 14:07
  • Had a look at the link, I am finding it difficult to use that to answer my question. Are you saying, functions, just like variables, as they are hoisted will be added to the scope chain? By scope chain I mean the property of an execution context. – Social Developer Jun 03 '21 at 14:40
  • @SebastianSimon please answer the question or reopen so others can answer. – Social Developer Jun 03 '21 at 14:56
  • All of `greet`, `f1` and `f2` are located in exactly the same scope. And this is the scope in which `f1` and `f2` are defined, to which the code in their body has access when the function is called. – Bergi Jun 03 '21 at 15:50
  • @Bergi, thanks, but through what medium did `f2` get access to `f1`? We know, when `f2` is invoked, an Execution Context (EC) is created. `greet` for instance is accessible via this EC's scope chain. So was `f1` in the scope chain as well? – Social Developer Jun 03 '21 at 16:12
  • Yes, as I said, all three variables are in the same scope, and if that scope is in the chain of your execution context, all three variables are accessible. – Bergi Jun 03 '21 at 16:13

0 Answers0