


What is the V8 JavaScript engine?
V8 is an open-source JavaScript engine developed by Google. It compiles and executes JavaScript code. It is used by Google Chrome and Node.js.
V8 converts JavaScript into machine code or other executable instructions so the computer can run it efficiently.
Remember: V8 executes JavaScript; Node.js provides the runtime environment and backend APIs.
What is Node.js?
Node.js is an open-source
cross-platform JavaScript runtime environment that allows us to run JavaScript outside the browser, mainly on the server side.
For example, we can use Node.js to build APIs, web servers, and backend applications.
Easy to remember: Node.js lets us run JavaScript outside the browser.
Why is Node.js used?
Node.js is used to build fast and scalable backend applications. It is especially useful for applications that handle many requests, such as chat applications, REST APIs, and real-time applications.
It also allows developers to use JavaScript for both frontend and backend development.
Examples:
Is Node.js a programming language?
No, Node.js is not a programming language. JavaScript is the programming language, and Node.js is the runtime environment that executes JavaScript outside the browser.
Remember: JavaScript is the language; Node.js is the runtime.
Is Node.js a framework?
No, Node.js is not a framework. It is a runtime environment. Frameworks like Express.js and NestJS run on top of Node.js and help us build backend applications more easily.
For example: we can use Node.js with Express.js to create APIs.
How does Node.js execute JavaScript?
Node.js uses the V8 engine to execute JavaScript. When asynchronous operations are needed, Node.js uses its runtime APIs and event loop to manage tasks without blocking JavaScript execution while waiting for many I/O operations to finish.
For example, when a server reads a file, Node.js can continue handling other work while waiting for the file operation to complete.
Simple flow:
JavaScript code
Node.js runtime + V8 engine
Execute code and handle asynchronous tasks
What are the key features of Node.js?
The key features of Node.js are:
Easy memory trick: Async, Non-blocking, Event-driven, Scalable, Cross-platform.
What is non-blocking I/O?
Non-blocking I/O means Node.js does not have to wait for an input/output operation, such as reading a file or fetching data from a database, before doing other work.
When the operation finishes, Node.js can process its result through a callback, Promise, or
async/await
Example:
const fs = require("node:fs");
fs.readFile("data.txt", "utf8", (err, data) => {
if (err) throw err;
console.log(data);
});
console.log("Hello");
Hello
(contents of data.txt)
Hello
is printed first because the file-reading operation completes asynchronously.
Remember: Non-blocking means don't wait unnecessarily; do other work while the operation is in progress.
What is synchronous vs asynchronous execution?
Synchronous execution means tasks run one after another, and the next task waits for the current task to finish.
Asynchronous execution allows us to start an operation and handle its result later, so other work can continue in the meantime.
Synchronous
Example:
readFileSync()
Asynchronous
Example:
readFile
// Synchronous
console.log("First");
console.log("Second");
// Asynchronous
console.log("First");
setTimeout(() => {
console.log("Third");
}, 1000);
console.log("Second");The asynchronous example prints
First
,
Second
, and then
Third
.
Remember: Synchronous = wait until finished. Asynchronous = handle the result later.