You have already learned the main basic concepts of JavaScript at the basic level, you can already
write some JavaScript codes, solve some problems.
You have already built a simple JavaScript application, and that's perfect, but not enough.
It's really good to learn how to write JavaScript code syntactically using a lot of interesting examples
and modern best practices.
But in most cases, you may be confused when you faced some real problems or errors in the future.
Therefore, in order to be a real and powerful JavaScript developer, you need to really understand
how JavaScript works, how the codes are executed.
In other words, you need to learn well how JavaScript acts behind the scenes.
We know that there is a built in JavaScript engine in the browser, which is used to execute JavaScript
code, let's describe how it actually happens.
So we have JavaScript code that is run in a Web browser.
And in every Web browser, there is a JavaScript engine.
Different Web browsers use different JavaScript engines, for example, Google Chrome uses V8 engine,
which was developed by Google and also since 2000 and 13 Opéra Web browser has been using it as well.
Then we have Spider Monkey, which powered Netscape and today powers Mozilla Firefox.
As for the Web browser Safari, it uses JavaScript core engine and the last one, Microsoft uses chakra
JavaScript engine.
OK.
It was just for your information, let's talk about the entire process of the jobs code is run and executed,
it consists of several steps.
At first the Web page is loaded and JavaScript code is run.
But in order to execute it and you should translate it to a browser because the Web browser itself cannot
understand JavaScript code directly.
But before JavaScript engine starts execution of the code, it should be checked syntactically.
And it's the main job of syntax parser, which is the part of JavaScript engine.
So the engine needs to start execution of the code that is syntactically acceptable for it.
So the syntax parser reads the code character by character and checks if its grammar is valid.
If there is something wrong, then it throws the syntax error.
OK, let's prove it in practice, go back to our text, Ed.
Credit variable where equals hello.
And then log it in console, console that log a.
Below, right function with name P.
But in this case, let's mhere writing parenthesis.
As you know, JavaScript engine executes code line by line.
So here we have variable AI equals to hello, then it's logged in Castle and then Syntex Parsons's function
B, but without parentheses.
So what do you expect to get?
You may expect hello.
And then an error because we are missing parentheses with functioning and you know that it's obviously
an error.
OK, let's see what we have.
We have immediately syntax error.
So what has happened, it was caused by the syntax parser and let me explain why before jobs engine
starts executing code line by line at first syntax parser has to read it and check if it's valid.
So it has started from variable Ecorse.
Hello.
It's correct.
And then it goes to the next line console that log a it's correct as well.
Then when it reaches the keyword function followed by its name be here, it expects to see either the
characters again or the parentheses, or at least the space before parentheses.
But instead of them, it finds curly braces, which is syntactically invalid, and therefore it throws
syntax error.
So you see that we got an error before the code was actually executed by the JavaScript engine.
All right, let's consider another important aspect when we talk about how JavaScript works behind the
scenes and its lexical scope, or sometimes it's called lexical environment.
So the lexical scope is an environment where a piece of the entire code physically sits.
JavaScript engine behaves in different ways, according to where this piece of code is placed.
For example, if the variable sits lexically inside the function, then we cannot access on that variable
outside this function.
In most cases, lexical scopes are defined by the functions.
So, again, this aspect is very important.
And regarding scopes and environments, we will talk about a little bit later.
OK, and the last thing I'm going to introduce is an execution context.
Execution context is like a container which managed the code that is currently running.
In most cases, it is created by the functions.
So in other words, which function is currently running then?
It creates execution context.
For now, just remember that if something in JavaScript is running, it's managed by execution context.
All right.
When we talk about how jobs it works behind the scenes, we have to figure out what these concepts mean
syntax, parser, lexical scope and execution context.
Let's move on.