JavaScript Execution & Asynchronous Concepts - Interview Notes
Promises:
A Promise represents a value that will be available later. It can resolve (success) or reject (failure).
Use .then() for success, .catch() for error, and .finally() for cleanup.
async / await:
async and await make it easier to handle Promises. async functions always return a Promise, and
await pauses execution until the Promise resolves.
Callback Hell:
Callback Hell happens when many nested callbacks make code hard to read. It's solved by using
Promises or async/await to write cleaner, flatter code.
call(), apply(), and bind():
Used to manually set 'this' in functions. call() and apply() execute immediately, while bind() returns a
new function with fixed 'this'. apply() takes arguments as an array; call() takes them individually.
Event Loop:
The Event Loop manages JavaScript's asynchronous behavior. It checks the Call Stack, Microtask
Queue, and Macrotask Queue, ensuring non-blocking execution.
Execution Priority:
JavaScript runs in this order: Synchronous -> Microtasks (Promises) -> Macrotasks (setTimeout) ->
Rendering. Promises have higher priority than timers.
Example Order:
[Link]('1'); setTimeout(()=>[Link]('2'),0); [Link]().then(()=>[Link]('3'));
[Link]('4'); Output: 1, 4, 3, 2
Most Asked JavaScript Interview Questions & Answers
Q1: What is the difference between var, let, and const?
-> var is function-scoped and can be redeclared. let and const are block-scoped. const values
cannot be reassigned.
Q2: What is hoisting in JavaScript?
-> Hoisting means variable and function declarations are moved to the top of their scope before
code execution.
Q3: What is the difference between == and === ?
-> == checks value only (performs type conversion). === checks both value and type (strict
equality).
Q4: What are arrow functions?
-> Shorter syntax for functions. Arrow functions do not have their own 'this'; they inherit from the
parent scope.
Q5: What is the difference between synchronous and asynchronous code?
-> Synchronous code runs line by line. Asynchronous code uses callbacks, promises, or async/await
to run later without blocking.
Q6: What is the difference between null and undefined?
-> undefined means a variable has been declared but not assigned. null means an intentional empty
value.
Q7: What are closures?
-> A closure is when a function remembers variables from its outer scope even after the outer
function has finished executing.
Q8: What is the use of the event loop?
-> It allows JavaScript to handle asynchronous tasks efficiently by managing call stack and queues.