Do you know how JavaScript works and how the code is executed? Is JavaScript synchronous or asynchronous? Is JavaScript single threaded or is it multi-threaded? Don't worry we'll cover everything, just keep reading techbrushup.
JavaScript is a synchronous single-threaded language.
JavaScript is Dynamically Typed means the type changes dynamically.
Synchronous by default → blocking, Executes code line by line.
Asynchronous → non-blocking (callbacks, promises, async/await).
How Does JavaScript Work?
Introduction to how JS works behind the scenes
JavaScript executes inside a JavaScript Engine (such as V8 in Google Chrome).
Browser JavaScript Engines
| Browser | JavaScript Engine |
|---|---|
| Google Chrome | V8 |
| Microsoft Edge | V8 |
| Mozilla Firefox | SpiderMonkey |
| Safari | JavaScriptCore (Nitro) |
| Opera | V8 |
The execution process is:
- 1. Parsing
- 2. Compilation
- 3. Execution.
Parsing in JS
The engine reads the JavaScript code and checks for syntax errors.
Compilation in JS
Modern engines compile the code into optimized machine code using Just-In-Time (JIT) compilation.
Execution Context in JS
Insight from our lab
Everything in JavaScript happens inside an Execution Context. JavaScript is not possible without this beautiful execution context.
JavaScript is not possible without this beautiful execution context.
What happens when you run JavaScript Code?
Do you know? When you run a JavaScript program, behind the scenes, the JavaScript engine creates an execution context where whole JavaScript code is executed.
You can assume this execution context to be a big box or a container in which JavaScript code is executed.
The execution context has two components in it.
The memory component is also referred to as the first component. The place where all the variables and functions are stored in key-value pairs.
The second component of this execution context is the code component. This is where code is executed one line at a time.
Modern JavaScript favors async/await for readability, but understanding all three patterns makes you a stronger developer.