Foundation: Javascript Runtime Flashcards
Is it only Javascript Engine which runs the code?
No, Javascript Runtime is another place when the JS code runs.
What is the difference between JavaScript Engine and JavaScript Runtime Environment?
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 does Javascript Runtime work?
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
Is browser a Javascript Runtime?
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 does setTimeOut works?
setTimeout is a Web API method that sets a timer which executes a function.
It’s async which uses Javascript runtime.
What is the runtime environment?
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.
What elements does browser runtime comprised?
The JavaScript engine (which in turn is made up of the heap and the call stack)
Web APIs
The callback queue
The event loop
Are node.js and browser runtime different?
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.
What are Web APIs?
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.
What are the types of Web APIs?
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.
What is callback queue?
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.
What is the job for event loop?
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 is Javascript running asynchronously if it’s single threaded?
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.