Node JS Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How does Node.js handle asynchronous operations?

A

Node.js is asynchronous and operates on a single thread that doesn’t have to wait for operations to complete before starting others. It uses an event queue to manage pending operations.

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

What type of projects is Node.js not suitable for?

A

Node.js is not suitable for high CPU-intensive projects like image processing, due to its single-threaded nature, which can block the event loop. It is better suited for network-intensive projects.

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

What language and engine is Node.js built on?

A

Node.js is built on C++ and uses Google’s V8 engine.

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

How do you run a JavaScript file with Node.js?

A

You can run a JavaScript file with Node.js by typing node <filename> in the terminal.

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

What is the equivalent of global objects in Node.js compared to the browser?

A

In the browser, global objects like console and setTimeout are prefixed with window (e.g., window.console.log). In Node.js, these objects are prefixed with global (e.g., global.console.log). Variables you define in the global scope are not prefixed.

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

What is the purpose of the main module in a Node.js application?

A

Every Node.js application has a main module. You can inspect its properties using console.log(module). This helps prevent variable conflicts because each file is a separate module.

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

How can you scan for errors in a Node.js application?

A

You can use jshint in the terminal to scan for errors in a Node.js application.

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

What does the module wrapper function in Node.js do?

A

Node.js wraps each module in a function with the signature function (exports, require, module, __filename, __dirname) { … } behind the scenes. This allows for module encapsulation.

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

Is require a global function in Node.js?

A

require seems like a global function, but it is not. It is provided to each module by the module wrapper function.

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

What is exports in Node.js?

A

exports is an alias for module.exports and is used to export functions and objects from a module.

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

What are the main phases of the Node.js event loop?

A
  1. Timers: Executes callbacks scheduled by setTimeout() or setInterval().
  2. Pending Callbacks: Executes I/O callbacks deferred to the next loop iteration.
  3. Idle, Prepare: Used internally by Node.js.
  4. Poll: Retrieves new I/O events and executes I/O-related callbacks.
  5. Check: Executes setImmediate() callbacks.
  6. Close Callbacks: Executes close callbacks, e.g., socket.on(‘close’, …).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the Timers phase in the Node.js event loop?

A

The Timers phase executes callbacks scheduled by setTimeout() or setInterval().

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

What happens during the Pending Callbacks phase?

A

I/O callbacks that were deferred to the next loop iteration are executed during this phase.

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

Explain the Poll phase in the Node.js event loop.

A

The Poll phase is responsible for retrieving new I/O events and executing I/O-related callbacks. It will block if there are no scheduled timers, waiting for I/O events.

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

When are setImmediate() callbacks executed in the Node.js event loop?

A

setImmediate() callbacks are executed during the Check phase of the event loop.

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

What is the role of the Close Callbacks phase?

A

The Close Callbacks phase handles the execution of some close callbacks, such as socket.on(‘close’, …).

17
Q

How does the Node.js event loop determine if it should exit or keep running?

A

Node.js will keep the event loop running as long as there are pending operations (e.g., I/O callbacks, timers, or callbacks in the task queue). It will exit when there are no more operations to perform.

18
Q

What is the function of the setTimeout(0) method in Node.js?

A

Using setTimeout(0) allows you to schedule a callback to be run after the current call stack is cleared, effectively running it in the next iteration of the event loop.

19
Q

How do worker threads interact with the main thread in Node.js?

A

Worker threads in Node.js perform operations in the background. The main thread is put to sleep and wakes up when there are events to process, a thread pool thread completes an operation, or the next timer goes off.

20
Q

How does the event loop in Node.js differ from the event loop in the browser?

A
  • Node.js: Utilizes C++ APIs for I/O operations.
  • Browser: Utilizes Web APIs for tasks such as AJAX requests and setTimeout.
21
Q

What happens when the event loop’s call stack is empty?

A

When the call stack is empty, the event loop checks the task queue and pushes the next callback onto the call stack to execute it.

22
Q

Explain the role of web APIs in the browser’s event loop.

A

The browser uses web APIs (e.g., AJAX, setTimeout) to handle asynchronous operations, allowing the single-threaded JavaScript runtime to offload tasks to these APIs and free up the call stack for other operations.