DEV Community

Cover image for It's all about the Javascript concept
Nazmus Sakib
Nazmus Sakib

Posted on • Updated on

It's all about the Javascript concept

High abstraction language

Javascript is a high-abstraction language. It does not need to manually manage the resource and the memory allocation, like in C, we have to manually do the resource management and memory allocation. But in javascript, we do need to do that. It does this by itself.

It also manages the Garbage collection by itself. Garbage collection uses a mark & sweep algorithm for it. It searches memory if any variable has been allocated but no pointer, it just marks it and sweeps it out from the memory.

Edge Case 

  1. Global Variable: Garbage collection won't remove this variable because it will call in every time in the global call, it just confused whether is it important or not.
  2. Event listener: Especially in SPA, it won't remove the unused variable.
  3. Set timer: Like before set timer context is not removed by garbage collection.

JIT

We all assume javascript is an interpreted language, but javascript is not a purely interpreted language. It's JIT(just in time) interpreted language.

JIT means that it is a mixture of compiled and interpreted language.

The interpreter tries to read the code and try to execute them one by one. First, it executes fast later on it became slow, on the other hand, the compiler takes time to compile for the first time later on it became faster.

So in this scenario, JIT comes into the place. it uses both an interpreter and a compiler simultaneously.
The interpreter executes code line by line and the compiler prefix looks for code that can be optimized, it sends the code to the compiler if the code is optimizable. After optimizing this, the code executes faster.

Paradigm

A paradigm is a mindset of code structure that determines your coding style.

There are different types of paradigms. Like

Procedural programming

  1. Object-oriented programming (OOP)

  2. Functional programming (FP)

Javascript support all of the paradigm. So we call javascript a multi-paradigm language.

Prototyped-Based

A prototype is a blueprint or design of a structure. If you think about the Array.prototype then we can see an Array has some methods like pop, Push, Index of, etc. If you try to build a new array, we can inherit the method from Array.prototype then. We use it in our arrays. So this is the design we follow from the prototype.

So, we can say that javascript is a prototyped-based language.

First-class citizens

In javascript, functions are first-class citizens.

It means that you can throw functions anywhere you want. You can treat the function as a variable. You can pass a function as a parameter of a function you can return functions from inside of another function. The feature keeps you a lot of flexibility while you using the function.

Like higher-order function, and the callback function.

Dynamically Typed

You don’t need to specify the type of the variable on the declaration time, it will know its type in the runtime

Single-threaded

Javascript is a single-threaded language. And single threaded means the execution of instruction happens in a single sequence.

Javascript Engine

In modern web browsers, there is a javascript engine. In Chrome, we call it the V8 engine. If any javascript code enters into the V8 engine then it parses the code, parses means the engine reads the code, and then it goes to the AST(abstract syntax tree). Then the engine starts to interpret the code line by line and the profiler starts to find the code which can be optimized. Then the execution part.
In Execution, there are call stack and memory heap.

Runtime

Javascript engine and runtime are different topics, they are not the same. If we use Google Chrome then the environment of Google Chrome is runtime. And the mechanism of Google Chrome is a javascript engine is a V8 engine. If we work on the local environment then Nodejs is the runtime.
Runtime has web API options like DOM, Fetch API, and Timer.

Synchronous / Asynchronous

Javascript is a core synchronous language. When javascript gets an execution request in the call stack then web API take it to the callback queue and the callback queue responds against the request. then event loops take back the successful response to the call stack to execute.
That's how javascript works in an asynchronous way.

Top comments (0)