Foundation: Javascript Runtime Flashcards

1
Q

Is it only Javascript Engine which runs the code?

A

No, Javascript Runtime is another place when the JS code runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between JavaScript Engine and JavaScript Runtime Environment?

A

The Javascript Runtime includes a set of APIs which helps us make asynchronous code execution possible.

Javascript runs in a container - a program that reads your js codes and runs them. This program must do two things:
1. parse your code and convert it to runnable commands
2. provide some objects to javascript so it can interact with the outside world.

The first part is called Engine, and the second is Runtime.
For example, the Chrome Browser and node.js use the same Engine - V8, but their Runtimes are different: in Chrome, you have the window, DOM objects etc., while the node gives you require, Buffers and processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does Javascript Runtime work?

A

In Chrome and Browser,
It have web API with DOM, fetch() and setTimeout()

The browser provides “windows”, these are APIs provided by web browser and these are asynchronous

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is browser a Javascript Runtime?

A

A browser contains a Javascript engine (for example Chrome v8).
The engine implements a Javascript runtime, which includes the call stack, heap and event loop.
The browser also usually includes a set of APIs that augment the Javascript runtime and make asynchronous code execution possible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does setTimeOut works?

A

setTimeout is a Web API method that sets a timer which executes a function.
It’s async which uses Javascript runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the runtime environment?

A

The JavaScript runtime environment provides access to built-in libraries and objects that are available to a program so that it can interact with the outside world and make the code work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What elements does browser runtime comprised?

A

The JavaScript engine (which in turn is made up of the heap and the call stack)
Web APIs
The callback queue
The event loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Are node.js and browser runtime different?

A

The runtime environment in a browser is very different from that of Node.js. These differences are primarily at the implementation level.
Yet, many concepts are similar.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are Web APIs?

A

The Web APIs are not a part of the JavaScript engine, but they are part of the runtime environment provided by the browser.
There are a large number of APIs available in modern browsers that allow us to a wide variety of things.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the types of Web APIs?

A

The most common categories of browser APIs let us:

Manipulate documents: DOM API
Drawing Graphics: Canvas API
Fetch from Server: Fetch API

Features like event listeners, timing functions and AJAX requests all sit in the Web APIs container until an action gets triggered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is callback queue?

A

The callback queue stores the callback functions sent from the Web APIs in the order in which they were added.
This queue is a data structure that runs first in, first out.

Callback functions will sit in the queue until the call stack is empty, they are then moved into the stack by the event loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the job for event loop?

A

The job of the event loop is to constantly monitor the state of the call stack and the callback queue.
If the stack is empty it will grab a callback from the callback queue and put it onto the call stack, scheduling it for execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is Javascript running asynchronously if it’s single threaded?

A

Even though it is a single-threaded language. JavaScript can only execute one function at a time, so this means it is synchronous, but as we can push callbacks from the Web APIs to the callback queue and in turn, the event loop can constantly add those callbacks to the call stack, we think of JavaScript as being able to run asynchronously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly