JavaScript working in Leyman words
Published: 2021-10-05 18:21:01


 
How do you think a JavaScript program is executed?
So every browser provides a JavaScript engine that runs the JavaScript code.
there are two methods commonly known, one is where the whole code file is run and transformed to machine language known as Compiled language and the other is a code file  executed line by line also known as interpreted type . so JavaScript is an interpreted language i.e. program is executed sequentially and hence it’s called  single-threaded language at runtime.


now, JavaScript called a Client-side Scripting Language. That means that it is a computer programming language that runs inside an Internet browser the browser provides JavaScript engine which is a program or an interpreter that executes JavaScript code. A JavaScript engine can be implemented as a standard interpreter, or just-in-time compiler that compiles JavaScript to byte code in some form.

The way JavaScript works is interesting. Inside a normal Web page you place some JavaScript code . When the browser loads the page, the browser has a built-in interpreter that reads the JavaScript code it finds in the page and runs it.

The engine used for JavaScript consists of two main components:

> Heap Memory— this is where the allocation of memory happens.
> Call Stack — this is the place where the stacks are getting called and the code executes. 

Like any other programming language, JavaScript runtime has one stack and one heap storage. A heap is a free memory storage unit where you can store memory in random order. Data that is going to persist in for a considerable amount of time go inside the heap. Heap is managed by the JavaScript runtime and cleaned up by the garbage collector.
What we are interested in is stack. A stack is LIFO (last in, first out) data storage that stores the current function execution context of a program.

so now imagine a small program consisting of a variable, function and o/p statement now the whole program is executed line by line where the variable encountered is stored separately  in the memory with value undefined (i.e all variables will be recorded in the memory at once that are existing in the whole program )and then next imagine a pointer which points to all different variable numbered sequentially. then when in the same program a function is encountered a separate execution context is created like a sub task and is executed line by line so if a variable is assigned a value in that function then after completing the function the subtask value is stored into the stack above the already existing element in the stack that is the Global Execution context  so this process repeats until there are more functions and the stack is accumulated accordingly. once the end is reached then the stack starts popping out the elements where this elements value is assigned to the  variable in the memory which was earlier undefined and  the process repeats till the stack is empty so all this happens simultaneously .

>Global execution context as an environment where the code runs.
>There can be multiple execution context in a program but a single Global execution context. 
>The stack is cleared after each operation as well as memory after the operation.

Overview

the program is divided into 3 parts that is the variable part ,the function part and the output part, these are the basic building blocks of a program where all data stored should be outputted at one point of the time. Now variables are stored in memory sequentially and when the function is encountered it is treated as subtask and the execution takes place separately for each function and pushed into the call stack and the value or outcome of that function is stored into stack then the same process completes till the end of the program and then the stack is popped and the associated value is assigned to related variables which were earlier undefined and the memory is erased after the value is outputted.