1. What is a Callback in JavaScript?
A callback is a function passed as an argument to another function and executed later, usually after an
asynchronous operation completes. Callbacks are commonly used in JavaScript for handling async tasks like
reading files, API calls, or timers.
2. Issues with Callbacks
Callbacks have several problems: 1) Callback Hell (deeply nested callbacks), 2) Poor readability and
maintainability, 3) Difficult error handling, 4) Inversion of Control, where you give control of your logic to
another function.
3. Inversion of Control with Callbacks
Inversion of Control means you lose control over when and how your callback is executed. You trust another
function (like an API) to call your callback correctly, which can lead to bugs, multiple executions, or no
execution at all.
4. How to Resolve Callback Hell
Callback Hell can be resolved using: 1) Promises, 2) async/await, 3) Modular functions, 4) Proper error
handling patterns.
5. What is a Promise in JavaScript?
A Promise represents a value that may be available now, later, or never. It has three states: pending, fulfilled,
and rejected.
6. ReferenceError: 'promise is not defined'
This error occurs when you try to use a variable named 'promise' that was never declared or is out of scope.
JavaScript is case-sensitive.
7. Should You Use 'Promise' or 'promise'?
'Promise' must be capitalized when creating a new Promise because it is a built-in class. 'promise' (lowercase)
is just a variable name.
8. Reusing the Variable Name 'promise'
You should avoid reusing the same variable name for multiple Promises, as it causes confusion and bugs due
to overwriting references.
9. Why Nothing is Printed in Promise Code
If nothing is printed, possible reasons include: 1) Promise is never resolved or rejected, 2) .then() or .catch() is
missing, 3) ReferenceError due to incorrect variable naming.
10. Normal Function vs Arrow Function
Key differences: 1) Arrow functions do not have their own 'this', 2) They do not have 'arguments', 3) They
cannot be used as constructors, 4) Shorter syntax.
11. setTimeout with var in a for loop
Using 'var' results in all callbacks sharing the same variable. By the time setTimeout executes, the loop has
finished, so the final value is printed multiple times.
12. setTimeout with let in a for loop
'let' creates a new block-scoped variable for each iteration, so the correct values (0 to 9) are printed.
13. JavaScript Event Loop Explanation
Synchronous code runs first in the Call Stack. setTimeout callbacks go to the Macrotask Queue. Promise
callbacks go to the Microtask Queue. Microtasks run before Macrotasks.
14. Where Are Functions Stored While Waiting?
Callbacks are stored in Web APIs first, then moved to Microtask Queue (Promises) or Macrotask Queue
(setTimeout).
15. async/await Output Order
async/await is syntactic sugar over Promises. Synchronous code runs first, await pauses execution, remaining
code runs via microtasks.
16. Event Bubbling
Event bubbling is a process where an event starts from the target element and propagates up to parent
elements.
17. Currying in JavaScript
Currying transforms a function with multiple arguments into a sequence of functions each taking one
argument.
18. SQL Query for Third Highest Salary
SELECT name, salary FROM employees e1 WHERE 2 = (SELECT COUNT(DISTINCT salary) FROM
employees e2 WHERE [Link] > [Link]);
19. Logging POST Request Data to a File
In [Link], use Express for routing and fs module to append user data to a [Link] file on POST requests.
20. JWT vs Sessions
JWT is stateless and stored client-side. Sessions are stateful and stored server-side.
21. Cookies vs Sessions
Cookies store small data on the client. Sessions store data on the server and use cookies for session IDs.
22. [Link] vs [Link]
[Link] are part of the URL path. [Link] comes after '?' in the URL.
23. libuv
libuv is a C library used by [Link] to handle asynchronous I/O, event loop, threads, and networking.
24. Sharding
Sharding is splitting a database into smaller parts to improve performance and scalability.
25. SQL vs NoSQL
SQL databases are structured and relational. NoSQL databases are schema-less and horizontally scalable.