Q. What are the tradeoffs in Event Delegation, Event Bubbling, and event capturing
π Event Propagation Phases
When an event occurs in the DOM, it goes through three phases:
-
Capturing Phase (a.k.a. "capture"):
- The event travels from the root (
document
) down to the target element.
- The event travels from the root (
-
Target Phase:
- The event reaches the target element.
-
Bubbling Phase:
- The event bubbles back up from the target to the root.
π Event Delegation
Event delegation is a technique where you attach a single event listener to a parent element instead of multiple children. It relies on event bubbling.
β Pros:
- Better performance (fewer listeners).
- Easier to manage dynamic elements (e.g., items added via JS).
- Cleaner, more maintainable code.
β Cons:
- Only works with events that bubble (e.g.,
click
, notfocus
orblur
). - Can be tricky with event.target vs. event.currentTarget.
Example:
document.getElementById('list').addEventListener('click', (e) => {
if (e.target.tagName === 'LI') {
console.log('Clicked:', e.target.textContent);
}
});
πΌ Event Bubbling (Default)
- Events bubble by default in JavaScript.
- Most common events like
click
,input
,submit
bubble.
Example:
document.body.addEventListener('click', () => {
console.log('Body clicked (bubbling)');
});
π½ Event Capturing
- You can listen during the capturing phase by passing
true
as the third argument toaddEventListener
.
Example:
document.body.addEventListener('click', () => {
console.log('Body clicked (capturing)');
}, true);
π§ͺ addEventListener
Syntax
element.addEventListener(type, listener, options);
-
options
can be:-
true
β use capturing -
false
(default) β use bubbling - or an object:
{ capture: true, once: false, passive: false }
-
Q. What are Workers and Service Worker, PWA Back ground Sync
- https://dev.to/ashutoshsarangi/web-worker-vs-service-worker-5h50
Q. Web Storage
- Web Storage (localStorage & sessionStorage)
- Cookies
- IndexedDB
- HTTP-only cookies
ποΈ 1. Web Storage
β localStorage
- Stores key-value pairs.
- Persistent (until manually cleared).
- Accessible via JavaScript.
- Client-side only.
β sessionStorage
- Similar to
localStorage
, but scoped to the browser tab/session. - Cleared when the tab is closed.
- Client-side only.
πͺ 2. Cookies
β Regular Cookies
- Can be read/written via JavaScript (
document.cookie
). - Sent with every HTTP request to the server.
- Can be scoped by domain/path/expiration.
π« HTTP-only Cookies
- Not accessible via JavaScript.
- Set by the server using the
Set-Cookie
header with theHttpOnly
flag. - Used for sensitive data like session tokens.
- Server-side only (client can store but not read/write).
Example:
Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Strict
π§ 3. IndexedDB
- A low-level API for storing large amounts of structured data.
- Supports transactions and complex queries.
- Asynchronous and event-driven.
- Client-side only.
Example:
const request = indexedDB.open("MyDatabase", 1);
request.onsuccess = (event) => {
const db = event.target.result;
console.log("DB opened:", db);
};
π Summary Table
Storage Type | Accessible via JS | Persistent | Size Limit | Server Access | Use Case |
---|---|---|---|---|---|
localStorage |
β Yes | β Yes | ~5MB | β No | Simple key-value storage |
sessionStorage |
β Yes | β No | ~5MB | β No | Per-tab/session data |
Cookies |
β Yes | β Yes | ~4KB | β Yes | Auth, preferences |
HTTP-only Cookies |
β No | β Yes | ~4KB | β Yes | Secure session tokens |
IndexedDB |
β Yes | β Yes | ~50MB+ | β No | Complex structured data |
Q. HTTP methods
π Common HTTP Methods
Method | Description |
---|---|
GET | Retrieves data from the server. Used for reading resources. |
POST | Sends data to the server, often used for creating resources or submitting forms. |
PUT | Replaces a resource entirely with new data. |
PATCH | Partially updates a resource. |
DELETE | Removes a resource from the server. |
HEAD | Similar to GET, but only retrieves headers (no body). Useful for checking metadata. |
OPTIONS | Describes the communication options for the target resource (e.g., allowed methods). |
CONNECT | Establishes a tunnel to the server, often used for HTTPS. |
TRACE | Echoes the received request, used for debugging. |
Q. Browser Apis
π§ Core Categories of Browser APIs
1. π Timer APIs
Used to schedule code execution.
-
setTimeout(fn, delay)
β runs once after delay. -
setInterval(fn, delay)
β runs repeatedly. -
requestAnimationFrame(fn)
β optimized for animations. -
queueMicrotask(fn)
β schedules a microtask (runs before next render).
2. π¦ Storage APIs
Used to store data on the client side.
-
localStorage
β persistent key-value storage. -
sessionStorage
β per-tab/session key-value storage. -
IndexedDB
β structured, large-scale storage. -
Cookies
β small key-value pairs, sent with requests. -
Cache API
β stores request/response pairs for offline use (used in service workers).
3. π Navigation & History APIs
Used to interact with the browser's navigation stack.
-
window.location
β read/write URL and redirect. -
history.pushState()
/history.replaceState()
β manipulate browser history without reload. -
popstate
event β detect back/forward navigation.
4. π DOM & Event APIs
Used to interact with the page structure and user actions.
-
document.querySelector
,getElementById
, etc. -
addEventListener
β handle events. -
MutationObserver
,IntersectionObserver
,ResizeObserver
β observe DOM changes.
5. π‘ Network APIs
Used to make HTTP requests and handle responses.
-
fetch()
β modern way to make requests. -
XMLHttpRequest
β legacy method. -
WebSocket
β real-time communication. -
navigator.sendBeacon()
β send data asynchronously (e.g., analytics).
6. π Security & Identity APIs
Used for authentication and permissions.
-
document.cookie
(with HttpOnly restrictions). Credential Management API
-
Permissions API
β check/request permissions (e.g., geolocation, camera).
7. π Device & Sensor APIs
Used to access hardware features.
navigator.geolocation
-
DeviceOrientationEvent
,DeviceMotionEvent
-
Battery API
,Clipboard API
,Vibration API
8. π§ Service Workers & Offline APIs
Used for background tasks and offline support.
ServiceWorker
CacheStorage
Background Sync
Q. Types of Observer
In JavaScript, observers are objects that watch for changes or events in the DOM or other browser APIs.
π Types of Observers in JavaScript
1. Intersection Observer
- Watches when an element enters or exits the viewport (or a parent container).
- Great for lazy loading images, infinite scrolling, or triggering animations.
Example:
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
console.log('Element is in view:', entry.target);
}
});
});
const target = document.querySelector('#myElement');
observer.observe(target);
2. Mutation Observer
- Watches for changes in the DOM (e.g., added/removed nodes, attribute changes).
- Useful for reacting to dynamic content updates.
Example:
const observer = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
console.log('Mutation detected:', mutation);
});
});
observer.observe(document.body, { childList: true, subtree: true });
3. Resize Observer
- Watches for changes in the size of an element.
- Useful for responsive design or layout adjustments.
Example:
const observer = new ResizeObserver((entries) => {
for (let entry of entries) {
console.log('Resized:', entry.target);
}
});
observer.observe(document.querySelector('#resizableElement'));
4. Performance Observer
- Monitors performance-related events like resource loading, long tasks, etc.
- Useful for performance analytics.
Example:
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach(entry => {
console.log('Performance entry:', entry);
});
});
observer.observe({ entryTypes: ['resource'] });
π§ JavaScript Event Loop Overview
JavaScript has two main task queues:
1. Macro Task Queue
- Includes:
setTimeout
,setInterval
, I/O, UI rendering. - These tasks are executed one at a time after the current stack is clear.
2. Microtask Queue
- Includes:
Promise
callbacks,queueMicrotask
,MutationObserver
callbacks. - These are executed immediately after the current task, before any macro tasks.
π Where Do Observers Reside?
β MutationObserver
- Microtask queue
- Its callback is scheduled as a microtask, meaning it runs before any
setTimeout
or other macro tasks.
β IntersectionObserver
- NOT in the microtask queue
- Its callback is scheduled as a task (macro task), and may be throttled by the browser for performance.
- It runs after rendering, not immediately after DOM changes.
β ResizeObserver
- Microtask queue
- Runs after layout and style calculations but before paint.
- Can be batched and delayed slightly for performance.
β PerformanceObserver
- Depends on the type of entry:
- Some entries (like
resource
) are delivered as macro tasks. - Others (like
longtask
) may be batched and delivered asynchronously.
- Some entries (like
Q. High Performance Browser Networking
- http1 vs http2
- Server Send Events
- webRTC
- polling β long polling vs short polling
- WebSocket
Q. debounce Vs Thruttling
const debFun = (fn, delay = 200) => {
let timeCounter;
return (...args) => {
if (timeCounter) {
clearTimeout(timeCounter);
}
timeCounter = setTimeout(() => {
fn(...args);
timeCounter = null;
}, delay);
};
};
const print = () => console.log('Hello');
const testFun = debFun(print);
setInterval(() => { testFun(); }, 300);
function throttle(fn, delay) {
let lastCall = 0;
return function(...args) {
const now = new Date().getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return fn(...args);
};
}
const print = () => console.log('Hello');
const throttledPrint = throttle(print, 200);
setInterval(() => {
throttledPrint();
}, 100);
Q. What are decorators, and how to create a custom decorator?
π§ What Are Decorators?
Decorators are a special kind of declaration that can be attached to classes, methods, accessors, properties, or parameters. They are used to modify or enhance behavior at runtime.
Decorators are currently a stage 3 proposal in JavaScript and are available in TypeScript or via Babel plugins.
π― Why Use Decorators?
Decorators help with:
- Code reuse: Apply common logic across multiple classes or methods.
- Separation of concerns: Keep business logic separate from cross-cutting concerns (e.g., logging, authorization).
- Meta-programming: Modify behavior dynamically.
β Example: Method Decorator in TypeScript
Hereβs a simple example of a decorator that logs method calls:
function Log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`Calling ${propertyKey} with`, args);
const result = originalMethod.apply(this, args);
console.log(`Result:`, result);
return result;
};
return descriptor;
}
class Calculator {
@Log
add(a: number, b: number) {
return a + b;
}
}
const calc = new Calculator();
calc.add(2, 3); // Logs method call and result
π§ͺ Output:
Calling add with [2, 3]
Result: 5
Top comments (0)