This repository is a collection of notes, explanations, and code examples for advanced JavaScript topics. It's designed to be a learning resource for developers looking to deepen their understanding of the language's core mechanisms and modern features.
A closure is a function bundled together with references to its surrounding state (the lexical environment). This means a closure gives you access to an outer function's scope from an inner function, even after the outer function has finished executing. The core idea is that the inner function "remembers" its environment.
A Promise is an object representing the eventual completion or failure of an asynchronous operation. A promise is in one of three states:
pending: The initial state, neither fulfilled nor rejected.fulfilled: The operation completed successfully.rejected: The operation failed.
The .then() method is used to schedule a callback to be executed when the promise is successfully resolved. The .catch() method is used to handle rejections or errors.
myPromise
.then((result) => {
// handle success
})
.catch((error) => {
// handle error
});Promise.all() takes an array of promises and returns a single promise. This returned promise resolves when all of the input promises have resolved, or it rejects as soon as one of the input promises is rejected.
The execution context is the environment in which JavaScript code is evaluated and executed. It's a crucial concept for understanding how the this keyword works and how variables are accessed.
The value of the this keyword is determined by how a function is called. It refers to the object that "owns" the code currently being executed. Its value can change depending on the context.
These are three powerful methods available on all functions that allow you to explicitly set the value of this for a function.
call(): Invokes the function immediately, allowing you to pass arguments one by one.apply(): Invokes the function immediately, but you pass arguments as an array.bind(): Returns a new function withthispermanently bound to the specified object. It does not invoke the function immediately.
JavaScript is single-threaded, but it uses the event loop to handle asynchronous operations.
- Call Stack: A LIFO (Last-In, First-Out) data structure that keeps track of the functions currently being executed.
- Event Loop: A mechanism that continuously checks if the Call Stack is empty. If it is, it moves functions from the callback queue to the Call Stack for execution.
Introduced in ES2017, async/await is a syntactic sugar on top of Promises that makes asynchronous code look and behave more like synchronous code.
async: Used to declare an asynchronous function. Anasyncfunction always returns a Promise.await: Pauses the execution of theasyncfunction until a Promise is settled (resolved or rejected) and gets its resolved value.
Every object in JavaScript has a prototype—a hidden internal link to another object. This link forms a chain, which is the foundation of prototypal inheritance.
When you try to access a property or method on an object, JavaScript first looks for it on the object itself. If it doesn't find it, it looks on the object's prototype, then the prototype's prototype, and so on, until it reaches the end of the chain (where the prototype is null).
This is a method by which objects can inherit properties from other objects. It's a core aspect of JavaScript and is often used as an alternative to traditional class-based inheritance.
Modules allow you to split your JavaScript code into separate, reusable files. There are two main module systems in the JavaScript ecosystem.
This is the standard module system for Node.js.
- Use
require()to import modules. - Use
module.exportsorexportsto export values from a module.
This is the official, standardized module system for JavaScript, supported by modern browsers and Node.js.
- Use
importto get functionality from another module. - Use
exportto expose functionality from a module.