DEV Community

Boomika N
Boomika N

Posted on

Java script Introduction

  1. What is JavaScript? JavaScript is a high-level programming language mainly used to make websites interactive and dynamic. Alongside HTML (structure) and CSS (style), it is one of the core technologies of the web. It allows you to: *Update content on a webpage without reloading *Handle user actions (clicks, typing, scrolling) *Create animations, games, and interactive UI *Communicate with servers (APIs)

JavaScript is often described as a single-threaded language, which means it can execute one task at a time on its main thread.
2.What does “Single-Threaded” mean?
A thread is like a path of execution in a program.
In JavaScript:
There is only one main thread
It executes code line by line, one task at a time
`Example:
console.log("Task 1");
console.log("Task 2");
console.log("Task 3");

Output:

Task 1
Task 2
Task 3

Each task waits for the previous one to finish.`

3.What is an Interpreted Language?
An interpreted language is a programming language where the code is executed line by line at runtime, instead of being converted into a separate machine code file beforehand.
How it works:
You write code
The interpreter reads and executes it line by line
No separate compilation step needed

4.What is Dynamic Typing?
A language is dynamically typed when:
You don’t need to declare the data type of a variable
The type is decided automatically at runtime.

5.What is a Java Script Engine?
A JavaScript engine is the program that reads, understands, and executes JavaScript code. Without a JS engine, your JavaScript code cannot run.
A JavaScript engine is a software component inside browsers (and runtimes) that:
Takes your JavaScript code
Converts it into machine-understandable instructions
Executes it
Examples of JS Engines:
V8 → used in Chrome and Node.js
SpiderMonkey → used in Firefox
JavaScriptCore → used in Safari

6.What is JIT Compiler?
A JIT (Just-In-Time) compiler is a technique used by modern JavaScript engines (like V8) to make code run faster by compiling it during execution.
JIT compiler = “compile while running”

Instead of:
Compiling the whole program before execution (like C/C++)
Or interpreting line by line (slow)

JIT does:
Converts JavaScript into machine code at runtime
Optimizes it while the program is running

7.Why JIT is Needed
JavaScript was originally:
Interpreted (slow)
Problem:
Repeated code runs slowly
No optimization

Solution:
JIT compiler improves speed by optimizing code dynamically

8.How JIT Compiler Works (Step-by-Step)
Let’s understand the full flow:
Step 1: Parsing (Code Understanding)
`Example:

function add(a, b) {
return a + b;
}`
Engine:
Checks syntax
Converts code into AST (Abstract Syntax Tree)
Now engine understands structure.

Step 2: Interpretation (Initial Execution)
Code runs using an interpreter first.
Why?
Faster startup
No waiting for compilation
But:
Not optimized → slower execution

Step 3: Profiling (Monitoring Code)

Engine tracks:
Which functions run often
Which loops repeat many times
This frequently used code is called:“Hot Code”
`Example:

for (let i = 0; i < 10000; i++) {
add(i, i);
}

add() becomes hot code.`

Step 4: JIT Compiles It
Now JIT converts this function into:
➡ Machine code (optimized)

Step 5: Optimization Techniques
JIT uses smart tricks to speed things up:

  1. Inline Caching
    Stores repeated operations
    Example:
    obj.name;
    obj.name;

    Engine remembers property location → faster access

  2. Function Inlining
    Replaces function call with actual code
    Instead of:
    add(2, 3);
    Engine may convert to:
    2 + 3;
    Removes function call overhead

  3. Type Optimization
    JavaScript is dynamic, but JIT assumes types:
    Example:add(2, 3); // numbers
    Engine assumes:
    a and b are numbers → uses fast numeric operations
    Engine assumes:
    a and b are numbers → uses fast numeric operations

Step 6: Execution of Optimized Code
Now:
Optimized machine code runs
Performance improves significantly

Step 7: De-optimization (Important)
If assumptions fail:
Example:
add(2, 3); // numbers
add("a", "b"); // strings

Problem:
JIT assumed numbers
Now types changed

Engine:
❌ Discards optimized code
✔ Falls back to interpreter

This is called:
De-optimization

Top comments (0)