node Flashcards

1
Q

what is node js

A

javscript runtime environment that runs javascript outside of browser. its built in google chromes v8 engine

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

is it single thread?

A

yes. io operations can be run separate from the main application flow. it runs on a single thread. so multiple requests can be managed effectively.without managing threads

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

event loop

A

when node js starts, it initializes the event loop. io operations or http requests are processed as and when they occurs. they run separately from main thread so concurrency acheived.

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

phases of event loop

A

timers, check, idle, prepare, io callbacks, poll, close callbacks etc

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

event queue

A

stores incoming events in order, and pass them one by one to event loop

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

api pool

A

it has all apis by node js to execute blocking events async. if something is a blocking event, then it is given to api pool and once it is processed what it was waiting for, then it enters the event queue

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

let and var

A

Summary
Scope: var is function-scoped, while let is block-scoped.
Hoisting: Both var and let are hoisted, but let has a temporal dead zone.
Redeclaration: var allows redeclaration within the same scope; let does not.
In general, it is recommended to use let for variable declarations to avoid issues related to hoisting and redeclaration, and to benefit from block scope.

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

event emitters and listeners

A

myemitter = new EventEmitter()
myemitter.on => listener - emit multiple times
myemitter.once => listener - emit only once
myemitter.emit => calling emitter

// Listen for the ‘eventName’ event
myEmitter.on(‘eventName’, (data) => {
console.log(Event received with data: ${data});
});

// Emit an event named ‘eventName’ with data
myEmitter.emit(‘eventName’, ‘some data’);

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

exports

A

for creating modules. each file is treated as module.

// math.js
exports.add = (a, b) => a + b;
exports.subtract = (a, b) => a - b;

// Usage in another file
const math = require(‘./math’);
console.log(math.add(2, 3)); // Output: 5
console.log(math.subtract(5, 2)); // Output: 3

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