Hey everyone. I decided to write a series about the most popular web language - JavaScript.
The series will be addressed mainly to people who start their adventure with programming and instead of reading another guide on how to iterate over the array, create functions, would like to get some information about how JavaScript works under the hood. The series will be divided into many small parts. I plan that the next parts will be released regularly in up to few days.

In the first part, I want to deal with three issues, which are the basis for understanding how JavaScript works.

  1. Syntax Parser
  2. Execution Contexts
  3. Lexical Environment

Syntax Parser

It is simply a program that reads your code and decides what it does and whether the code you wrote is grammatically correct.
When you write a line of code, the computer, seeing all the functions and loops, does not quite know what to do.
Fortunately, there are programs responsible for the "translation" of what has been written by us into a language that the computer will understand. Such programs are called compilers.
Our code is read character by character and checked if the syntax and the code are correct.  After that compiler does its job and compiles what has been written in a way that the computer can understand.

You can think about your code in the following way:

  • You have your code.
  • There is a program that converts what you wrote into a set of instructions that will be understood by the computer

  • So for example, if we have the above-mentioned function. This function will be represented in the computer's memory and translated from a more human-friendly code (JavaScript) into a language intelligible by hardware.

Lexical Environment

Lexical Environment exists in languages in which it matters where the code is written.
When it comes to Lexical Environment, for example, of a function or a variable, we are really talking about where they will be located in the computer's memory after compilation. How they will interact with other functions, variables or elements of the program.
Simply "Where is it written and what surrounds it".

For example in the example above, the function first and second sit both lexically in the same place. Same goes for variable a and variable b. On the other hand in function second, variable c and d do not belong to the same lexical environment as variable e because it was created inside function third.

Execution Context

Execution context is an abstract concept used by the ECMAScript specification. Everytime a script is run a Global Execution Context is created, also everytime a function is run a Execution Context of that function is created too. As you can see in the above example we can find a lot of Lexical Environments in a simple script, however, which one is currently running is defined by the Execution Context. I will talk a lot about it in later parts of this blog.

Next Post

I'm going to talk about basics information about Objects and NameValue Pairs in Javascript