Get ready for your next interview with our comprehensive question library
V8 is Google's open-source JavaScript engine written in C++. It compiles JavaScript directly to native machine code before executing it, making it very fast.
Node.js uses V8 to:
V8 features include just-in-time (JIT) compilation, hidden class optimization, and efficient garbage collection.
Non-blocking I/O means that I/O operations (like reading files, database queries, network requests) don't stop the execution of other code. Instead of waiting for an operation to complete, Node.js continues executing other code and handles the I/O result when it's ready.
// Non-blocking example
const fs = require('fs');
console.log('Start reading file');
fs.readFile('large-file.txt', (err, data) => {
console.log('File read complete');
});
console.log('This executes immediately');
// Output: Start reading file, This executes immediately, File read complete
This allows Node.js to handle thousands of concurrent connections efficiently.
Callbacks are functions passed as arguments to other functions, executed when an asynchronous operation completes. Callback hell occurs when multiple nested callbacks make code difficult to read and maintain.
// Callback hell example
getData((err, data) => {
if (err) throw err;
processData(data, (err, processed) => {
if (err) throw err;
saveData(processed, (err, result) => {
if (err) throw err;
console.log('Data saved successfully');
});
});
});
Solutions include Promises, async/await, and modularizing code into smaller functions.
// CommonJS (require)
const fs = require('fs');
const { readFile } = require('fs');
// ES6 Modules (import)
import fs from 'fs';
import { readFile } from 'fs';
// Dynamic import
const module = await import('./dynamic-module.js');
Key differences:
// Method 1: Using exports (shorthand)
exports.add = (a, b) => a + b;
exports.subtract = (a, b) => a - b;
// Method 2: Using module.exports
module.exports = {
add: (a, b) => a + b,
subtract: (a, b) => a - b
};
// Method 3: Exporting a single function
module.exports = (a, b) => a + b;
// Wrong: This breaks the reference
exports = { add: (a, b) => a + b }; // Won't work
npm (Node Package Manager) is the default package manager for Node.js, used to install, manage, and share JavaScript packages.
npm scripts are commands defined in package.json that can be executed using npm run <script-name>
:
{
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest",
"build": "webpack --mode production",
"lint": "eslint .",
"prestart": "npm run build"
}
}
Benefits:
package.json: Contains project metadata, dependencies, and scripts
{
"name": "my-app",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"nodemon": "^2.0.15"
}
}
package-lock.json: Locks exact versions of dependencies and their sub-dependencies for reproducible builds
Node.js provides both synchronous and asynchronous methods for file operations:
const fs = require('fs');
// Asynchronous (recommended)
fs.readFile('input.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log('File content:', data);
});
fs.writeFile('output.txt', 'Hello World', (err) => {
if (err) throw err;
console.log('File written successfully');
});
// Synchronous (blocking)
try {
const data = fs.readFileSync('input.txt', 'utf8');
fs.writeFileSync('output.txt', data.toUpperCase());
} catch (err) {
console.error('Error:', err);
}
// Promise-based
const fsPromises = require('fs').promises;
const data = await fsPromises.readFile('input.txt', 'utf8');
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime