| Feature | var | let |
|---|---|---|
| Scope | Function Scoped | Block Scoped |
| Hoisting | Variables are hoisted and initialized to undefined. | Variables are hoisted but not initialized |
| Re-Declaration | Can be redeclared within the same scope | Can't be redeclared |
| Temporal Dead Zone | Doesn't have a Temporal Dead Zone | Has a temporal Dead Zone |
- JS is single-threaded but uses event loop to handle async tasks.
- Queue (callbacks, promises) → Event Loop → Call Stack.
- Ensures non-blocking execution.
- == → Loose equality (performs type coercion).
- === → Strict equality (no coercion).
"5" == 5 // true
"5" === 5 // falseFunction remembers variables from its lexical scope even after outer function execution ends.
function outer() {
let count = 0;
return function inner() {
return ++count;
}
}
const counter = outer();
counter(); // 1
counter(); // 2- Variables (
var) and functions are hoisted to the top of their scope. varhoisted with undefined,let/consthoisted but in temporal dead zone.
- Synchronous → Executes line by line, blocking.
- Asynchronous → Doesn’t block, uses callbacks, promises, async/await.
- Promise → Handles async results (
then/catch). - async/await→ Cleaner syntax, built on top of Promises.
async function getData() {
const data = await fetch(...);
}- map → Transforms array, returns new array.
- forEach → Iterates, no return value.
- filter → Returns subset matching condition.
- reduce → Accumulates to a single value.
- Debounce → Delay execution until after a pause in events.
- Throttle → Execute at most once in a given interval.
- In object → Refers to object.
- In function → undefined in strict mode, else global object.
- In arrow function → Lexically bound (inherits from parent).
const obj = {
name: "JS",
normal: function() { console.log(this.name); },
arrow: () => console.log(this.name)
};
obj.normal(); // "JS"
obj.arrow(); // undefined