Skip to content

Instantly share code, notes, and snippets.

View RajnishXCode's full-sized avatar
🎯
Focusing

Rajnish Tripathi RajnishXCode

🎯
Focusing
View GitHub Profile
| **Concept** | **Details** |
|-------------------|------------------------------------------------------------------------------|
| **Promise State** | `pending`, `fulfilled`, `rejected` |
| **Microtask** | `.then`, `await`, `queueMicrotask()` |
| **Task** | `setTimeout`, `setInterval`, `fetch` |
| **Event Loop** | Runs call stack β†’ microtasks β†’ tasks |
| **async/await** | Syntactic sugar for Promises |
| **Error Handling**| Use `.catch()` or `try/catch` |
## JavaScript `this` Binding β€” At a Glance
| How You Call It | What `this` Refers To |
|-------------------------------------|----------------------------------|
| `regularFunction()` | `global` / `undefined` |
| `obj.method()` | `obj` |
| `arrowFunction()` | Inherited from lexical scope |
| `method.call(obj)` / `apply(obj)` | `obj` |
| `method.bind(obj)` | `obj` (returned function bound) |
| `setTimeout(function)` | `global` / `undefined` |
| Syntax | What `this` refers to |
|-----------------------------|---------------------------------|
| Regular function | `undefined` (strict) / `window` |
| Object method | The object |
| Arrow function | Lexical surrounding scope |
| `call(obj)` / `apply(obj)` | `obj` |
| `bind(obj)` | `obj` (returns new function) |
| `setTimeout` (regular fn) | `undefined` / `window` |
| `setTimeout` (arrow fn) | Inherits from outer context |
| Concept | Key Idea |
|--------------------------|--------------------------------------------------------------------------|
| Scope | Controls visibility of variables |
| Lexical Scope | Inner functions access variables from parent scopes |
| Hoisting | Declarations are moved to top before execution |
| TDZ (Temporal Dead Zone) | `let`/`const` exist but aren’t usable until initialized |
| Shadowing | Inner scope overrides variables with same name |
| Module Scope | JS Modules have isolated scope; vars aren't global unless exported |
| Call Stack | JS engine's way to manage function calls using LIFO |
| Scope Type | Description |
|--------------------|-----------------------------------------------------------------------------|
| **Global Scope** | Accessible everywhere (outside any function or block) |
| **Function Scope** | `var` declarations inside a function are limited to it |
| **Block Scope** | `let` and `const` inside `{}` blocks are accessible only inside the block |
| **Lexical Scope** | Inner functions access outer variables from where they were defined |
| **Module Scope** | In ES Modules, variables are scoped to that file unless exported |
| Feature | Function Declaration | Function Expression | Arrow Function |
|--------------------------------|-----------------------|----------------------|---------------------------|
| **Named** | Yes | Optional | Optional |
| **Hoisted** | Yes | No | No |
| **Lexical `this`** | No | No | Yes |
| **Can be used as constructor** | Yes | Yes | No |
| **Can be anonymous** | No | Yes | Yes |
| **Common Use** | General purpose | Callbacks | One-liners, Array methods |
| Concept | Key Insight |
|---------------------------------|------------------------------------------------------------------------------|
| **Closure** | Inner functions keep reference to outer variables |
| **Callback** | A function passed to another function |
| **HOF (Higher-Order Function)** | A function that takes or returns another function |
| **Execution Context** | Each function call creates a new isolated scope |
| **Microtask vs Task Queue** | Promises (microtasks) are prioritized and run before setTimeout (task queue) |
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ JavaScript Code β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Call Stack β”‚ ◄────────────────────────────┐
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚
β–Ό β”‚
## JavaScript Memory: Stack vs Heap
| **Stack** | **Heap** |
|----------------------------------|---------------------------------------------------|
| Fast, simple memory storage | Large, dynamic memory |
| Holds **primitive** values | Holds **reference** values (objects, arrays) |
| Copy by **value** | Variables store **memory addresses (references)** |
## JavaScript Key Concepts – Day 1 Summary
| Term | Meaning |
|-----------------|---------------------------------------------------------|
| `var` | Function scoped, hoisted with `undefined` |
| `let` / `const` | Block scoped, exist in Temporal Dead Zone (TDZ) |
| Primitives | Copied by value (stored in the stack) |
| References | Copied by reference (stored in the heap) |
| Scope Chain | Functions access variables from outer scopes |
| Closure | Functions remember outer variables even after execution |