JAVASCRIPT ENGINE
All our devices have a component called a microprocessor that is the brain of the devices themselves and is responsible for executing the instructions that are in memory.
THE MACHINE CODE
Machine language or machine code is a long series of 0s and 1s, electrical signals that the processor processes to give us output. Over the years, languages have developed that are gradually more and more distant from machine code but understandable to us humans. These languages to be executed by the processor must go through the reverse process, that is, the transformation from high-level code to machine code.
ENGINE
In the case of both client-side and server-side javascript, this translation work is done by the engine (javascript engines).
V8 ENGINE
As for client-side JS, the code runs in the browser and the V8 Engine cannot interact with the file system or access databases, browsers ensure that the JS code does not operate outside the browser itself.
V8 CLIENT-SIDE AND SERVER-SIDE ENGINE
However, we also know that C++ can interact with the file system; therefore, we can take the V8 engine add C++ code and make sure that it can interact with file systems and databases. This is what happens with Node.js.
JS IS A DYNAMIC TYPING LANGUAGE
Javascript was not born as a fast language, far from it, is dynamic typing i.e. at the time of declaring a variable you do not use the type as you do in other languages such as C, this is not a good thing for the compiler because it has to understand the data type and this affects speed.
THE JIT COMPILER (JUST IN TIME)
However, JS while being dynamically typed is fast enough due to the cooperation of the javascript interpreter and a compiler JIT. Interpreter and compiler work symbiotically to optimize code execution.
EXECUTION CONTEXT AND GLOBAL EXECUTION CONTEXT
Hoisting does not occur for variables defined with let or const because in the creation phase they are not put in memory. Initially, the javascript engine defines the global execution context in two steps. One step called Creation Phase and the other Execution Phase. In the execution phase step, the code is executed line by line. When the engine encounters a function definition a function execution context is created always in the two steps of creation and execution.
THE RUNTIME.
Runtime literally means execution time. The runtime consists of two components, HEAP a memory area where variables and functions are placed and a EXECUTION STACK, a structure of type LIFO (Last In First Out) i.e., the last element that entered is the first to exit. Suppose we need to carry out the instructions shown in the figure below.
OPERATIONS PERFORMED BY THE RUNTIME
The runtime performs the following operations:
- Places global execution context at the bottom of the stack
- Hoisting of functions a and b is done by placing them in the heap
- The variable v2 defined with var is also hoisted and assigned the value undefined
- Then the javascript engine starts executing the code line by line, assigns the value 10 to v1 and the value 20 to v2 by placing the variables in the heap
- It then arrives at the invocation of a() and creates an execution context for a() again with the two creation phase and execution phase and places it on the stack
- Function a() invokes function b() an execution context is then created for function b() and is placed at the top of the stack
The image shows the runtime in the steps described.
- Then the function console.log(‘Hello from b); after being executed is removed from the stack
- The pointer to the stack now points to b() which is leaving its scope of visibility. Then b() ends
- In the stack now there is a() that executes a console.log that is put at the top of the stack.
- Upon completion it is removed.
- Also a() has terminated its instructions, so its execution context is removed from the stack
The stack allows the javascript engine to keep track of execution contexts and at any given time know which context it should execute.
JAVASCRIPT IS A SINGLE THREAD LANGUAGE
View the code below for an explanation of this topic. Basically when dealing with very long operations the user interface is frozen.
SCOPE
In JS we have two types of scope, Global scope and Local scope. Global scope is unique for the entire document and exists from the beginning to the end of the Javascript program. The local scope is created each time a new function is defined.
BLOCK SCOPE AND DIFFERENCE BETWEEN VAR, LET AND CONST
The block scope is not supported by var, but it is supported by let and const. Having a variable visibility rule helps us write safe code. It is useful to enclose variables in a block because we have a small space reserved with variables that are born and die.
Leave A Comment