31. What is a rest parameter?
Rest parameter (...args) allows a function to accept an indefinite number of arguments as an array.
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
32. What are template literals?
Template literals use backticks (`) to embed variables and expressions.
const name = 'Alice';
console.log(Hello, ${name}!); // Hello, Alice!
33. What is a module in JS?
Modules help organize code into reusable files using
export and import. // file.js
export const greet = () => console.log('Hi');
// main.js
import { greet } from './file.js';
greet();
34. Difference between default export and named export
• Default export: One per file, imported without
{}• Named export: Multiple exports, must use the same name
// default export
export default function() {}
import myFunc from './file.js';
// named export
export const x = 5;
import { x } from './file.js';
35. How do you handle errors in JavaScript?
Use
try...catch to handle runtime errors gracefully. try {
JSON.parse("invalid json");
} catch (error) {
console.log("Caught:", error.message);
}
36. What is the use of try...catch?
To catch exceptions and prevent the entire program from crashing. It helps with debugging and smoother user experience.
37. What is a service worker?
A script that runs in the background of web apps. Enables features like offline access, caching, and push notifications.
38. localStorage vs. sessionStorage
•
localStorage: Data persists after tab/browser close•
sessionStorage: Data clears when the tab is closedlocalStorage.setItem('user', 'John');
sessionStorage.setItem('token', 'abc123');
39. What is debounce and throttle?
• Debounce: Waits for inactivity before running a function
• Throttle: Limits function execution to once per time interval
Used in scroll/input/resize events to reduce overhead.
40. What is the Fetch API?
Used to make network requests. Returns a Promise.
fetch('https://api.example.com')
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));
💬 Double Tap ❤️ for Part-5!