How does Javascript run the code?

How does Javascript run the code?

part 1:

Javascript is often very confusing. So I decided to get to know javascript a little better, here enjoy me asking different questions to the javascript.

Conversation with javascript...

  • Hey JS please describe yourself ?

    I am a single-threaded, non-blocking, synchronous, concurrent language. I have a call stack, an event loop, a callback queue, and some other APIs.

  • Where do you run your code?

    Mostly on engines like the V8 JS engine or SpiderMonkey which was written by my creator.

  • So they must support all that crap you were talking about?

    With me, you never know. Go and ask the engines.

Dumb V8

  • Hey V8 JS is saying it has a call stack, an event loop, a callback queue, and some other APIs, etc. Do you know about this?

    I have a call stack and a heap. Everything else you said went over my head.

  • So have you heard anything about setTimeout(), DOM or maybe HTTP request?

    No man I am not that smart I suppose. You tell me what are those.

So basically they are the APIs I was asking you about which are provided by browser.

Further investigation

image.png

Looks like V8 is not that smart after all. I got the blueprint of the whole structure of javascript just by googling it.

It looks like Chrome knows all the details:

image.png

  1. Hey JS could you please explain each term you said in your definition to me like I am 5?

    Okay, let me explain you each and every term in detail. I will be explaining the following terms in detail.

  - Single-threaded 

 - Non-blocking

  - Synchronous

 - Concurrent 
  • Single-threaded:

I do one thing at a time

I hope that answers your question. I can do only one thing at a time as I have only one call stack and one heap.

  • Non-blocking:

im-pretty-efficient-when-it-comes-to-delegating-tasks-a872e.webp

Okay, so I can do one thing at a time. So I can not assign my time to time-consuming tasks. So I use the help of a browser to get those tasks done and till that time I can work on other tasks. That part of asynchronous behavior is called non-blocking.

  • Synchronous:-

image.png

I prefer order instead of chaos I will attend to the tasks with help of a queue only which is apparent in the pictures.

  • Concurrent:-

The concurrent nature is mostly because of the Browser API/Web API events. These include methods like setTimeout, or event handlers like click, mouse over, scroll, and many more. You can use promises as well to perform asynchronous tasks using javascript.