> For the complete documentation index, see [llms.txt](https://bharat-tiwari.gitbook.io/our-tech-journal/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bharat-tiwari.gitbook.io/our-tech-journal/javascript/javascript-code-execution.md).

# Javascript Code Execution

## Javascript Statements

* code statements that just are there in the memory&#x20;
* can be used to do some computation / logic, but doesn't return anything e.g. if-statement or function body

## Javascript expressions

* code that returns some value from an assignment or other operator

  e.g. \* a=3; // returns 3

  * b === 'abc'; //returns true/false
  * anonymous function
  *

## JS Execution is single threaded

Single threaded = one command at a time&#x20;

Some languages are multi-threaded, that means the language might be running multiple pieces of code in multiple threads in  parallel at a time.

But javascript is single threaded

(the Browser program , under the hood, may give multi-threaded behavior to the JS code, but for our/developer's perspective JS is running in single thread)

## JS Execution is synchronous

Synchronous execution = one line of code at a time and in the order(top to bottom)

Javascript execution is synchronous always&#x20;

(Asynchronous JS code execution for example like in AJAX feels like or behaves like Asynchronous but there too javascript execution is synchronous)

## Code Execution flow

* Top to bottom
* Two cycles
  * 1st cycle ( also called **Hositing**)
    * starts from the top of the code to bottom
    * goes through all the code and puts all variables/object/function definitions in memory as properties of 'global' object
    * skips any execution or evaluation code, e.g. a function call or boolean evaluation or assignment is skipped
  * 2nd cycle:
    * starts from the top of the code to bottom
    * starts execution and evaluation of any function calls, or assignments or evaluations.

![](https://1265790178-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0HLhUbeW2DVd3onhHh%2F-M0HLilSkPXld4qvUaZ-%2F-M0HLwF746g6CnF0fTI8%2FjsCodeExecution.png?generation=1581929722199349\&alt=media)
