In the current post I would like to develop some of what was discussed in part 3.
Now that our file looks like this, let's think about what we actually wrote.
- 1st line is variable a.
- 3rd line is b function
- 7th line is invoking our function b
- 8th line is console logging variable a.
If someone has not seen this method before, the simplest explanation is that The console.log () method writes a message to the console. That's it.
Now that we understand every part of our code, after looking at our console in the browser, we'll see two things.
This is of course due to the console.log () method
Ok, now we will modify our one.js file again.
Surprise! Our code was run successfully. We received "I'm inside b function", and undefined instead of error. So even though our function is below where it's executed it still returned what we wanted to console.log.
Also when we look at the variable, it is still available for us. Only when we remove a variable completaly it will give us an error.
To understand how this is all possible, we should dig deeper into the Execution Context. We are going back to Execution Context again. It has two phases. We will focus on the first one.
As you recall from the previous section, along with Execution Context that wrapps our code we get the Global Object and special variable: this.
When it comes to the functions, hoisted is function's name the actual function definition.However, function definition hoisting only occurs for function declarations, not function expressions. But I will talk about them in the later parts of the blog.
I will talk about Execution Context second phase: Execution Phase and undefined