An in-depth developer's handbook to JavaScript. Understanding closures, prototype scopes, async promise loops, the V8 engine, event loops, and web performance optimization.
.then(), await) are executed in the microtask queue, which has priority over the standard callback task queue.ES6 (ECMAScript 2015) and subsequent updates transformed JavaScript, introducing features like block scoping, arrow functions, destructuring, and classes.
// Modern array destructuring and template literals
const profile = { name: "Ajit Dev", stack: ["Node.js", "React"] };
const { name, stack } = profile;
console.log(`${name} builds applications using: ${stack.join(", ")}`);
The DOM is the browser's in-memory tree representation of the webpage.
JavaScript handles async operations using Promises and async/await syntax wrappers.
// Fetch API usage with async/await
async function fetchDeveloperData(username) {
try {
const response = await fetch(`https://api.github.com/users/${username}`);
if (!response.ok) throw new Error("Network issues!");
const data = await response.json();
return data;
} catch (err) {
console.error("Fetch failed:", err);
}
}
fetchDeveloperData("ajitdev01").then(data => console.log(data?.name));
JavaScript engines (like V8 in Chrome/Node) run on a single main thread using:
[ Call Stack ] <─── (Pushes Microtasks First) <─── [ Microtask Queue ]
│
▼ (Checks if Stack is empty)
[ Event Loop ] <─── (Pushes Macrotasks Next) <─── [ Macrotask Queue ]
A closure is created when an inner function retains access to its lexical outer function scope even after the outer function has completed.
function createCounter() {
let count = 0; // private state
return {
increment() {
count++;
return count;
},
getCount() {
return count;
}
};
}
const counter = createCounter();
console.log(counter.increment()); // 1
console.log(counter.increment()); // 2
Objects inherit properties directly from parent prototype references linked via the internal [[Prototype]] property (accessible via Object.getPrototypeOf).
const vehicle = {
hasEngine: true
};
const car = Object.create(vehicle);
car.doors = 4;
console.log(car.hasEngine); // true (inherited)
console.log(car.doors); // 4 (own property)
var and function declarations are moved (hoisted) to the top of their enclosing scope before execution. Variables declared with let and const are also hoisted but reside inside a Temporal Dead Zone (TDZ).null and undefined?
undefined indicates a variable has been declared but not assigned a value, whereas null is an assigned value representing the intentional absence of any object reference.