12 Advanced JavaScript Concepts - Detailed Summary
1. Scope
Scope defines where a variable can be accessed in your code. JavaScript has function scope and block
scope. `let` and `const` are block-scoped, while `var` is function-scoped and can lead to unexpected behavior
if not handled carefully.
2. Hoisting
In JavaScript, declarations (variables and functions) are moved to the top of their scope before code
execution. `var` is hoisted and initialized with undefined, while `let` and `const` are hoisted but not initialized,
leading to a Temporal Dead Zone.
3. The `this` Keyword
`this` refers to the execution context. In methods, it refers to the object. In global functions or in strict mode, it
can be undefined or the window/global object. It can be explicitly set using `call()`, `apply()`, or `bind()`.
4. Callbacks, Promises, and Async/Await
Callbacks are functions passed as arguments to be executed later. Promises allow chaining with `.then()` and
`.catch()`. Async/await makes asynchronous code look synchronous, improving readability and reducing
callback hell.
5. Lexical Environment & Closures
A lexical environment is the environment where variables are defined, based on the structure of code.
Closures are functions that retain access to variables from their lexical scope even after the outer function
has completed.
6. Currying & Partial Application
Currying transforms a function with multiple arguments into a series of unary functions. Partial application
allows pre-filling some arguments of a function, returning a new function waiting for the remaining arguments.
12 Advanced JavaScript Concepts - Detailed Summary
7. Event Loop
The event loop handles asynchronous operations by moving tasks between the call stack and message
queues. Microtasks (Promises, async/await) have priority over macrotasks (setTimeout, DOM events).
8. Prototypal Inheritance
JavaScript uses prototypes for inheritance. Objects can inherit properties and methods from other objects
through their prototype chain. This allows efficient method sharing and code reuse.
9. `this`, `new`, and Classes
`this` refers to the context; `new` creates a new instance of an object with a constructor. Classes are
syntactic sugar over prototypes, offering a cleaner, more structured approach to object creation and
inheritance.
10. Modules, Imports/Exports, and Encapsulation
Modules allow you to split code into reusable pieces using `export` and `import`. This helps with
encapsulation, scope control, and better project structure.
11. Garbage Collection & Memory Management
JavaScript automatically reclaims memory using garbage collection, particularly the mark-and-sweep
algorithm. Developers must avoid memory leaks by clearing intervals, event listeners, and avoiding lingering
references.
12. Event Delegation
Event delegation uses a single event listener on a parent element to manage events on its children, taking
advantage of event bubbling. This improves performance and handles dynamic elements efficiently.