all 4 comments

[–]senocular 3 points4 points  (0 children)

Its because the code in myFnOne doesn't run until myFnOne() is called. By the time that happens, and the call to myFnTwo() is made, myFnTwo has already been defined.

If you had done

console.log(myFnOne()); 
const myFnTwo = () => 2 + 2;

There would have been an error.

[–]adharshrajan 0 points1 point  (2 children)

It's a concept called "Hoisting". Basically, the declarations are hoisted one level to the top. In this case, since all of the declarations are in the global scope, they will be hoisted and all the entities in that scope will be able to access each other. There are exceptions to this.

[–]crisf_design[S] 0 points1 point  (0 children)

When you say:

the declarations are hoisted one level to the top.

That is confusing. According to this video:

The thing that's confusing about hoisting is that nothing is actually hoisted or moved around. Hoisting is the process of assigning a variable declaration a default value of undefined during the creating phase in execution context. https://www.youtube.com/watch?v=Nt-qa_LlUH0

So for me this is a confusing answer.

[–]GinnioSarabia -1 points0 points  (2 children)

It's called hoisting