Scope, closures, this, prototypes, promises, event loop
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freevar, let, and const?var is function-scoped and hoisted, while let and const are block-scoped. const also prevents reassignment. Only var is initialized to undefined immediately; let and const are not accessible before their declaration due to the temporal dead zone.
A closure is a function that remembers the variables from its lexical scope even after that scope has finished executing. This allows functions to access variables defined outside their immediate scope.
this behave in arrow functions compared to regular functions?Arrow functions don't have their own this; they inherit it from the surrounding scope. Regular functions have their own this, which depends on how they're called.
=== do differently from ==?=== checks for strict equality without type coercion, so both value and type must match. == allows type coercion, which can lead to unexpected results.
let and const.The temporal dead zone is the period between entering a scope and the actual declaration of a let or const variable. During this time, accessing the variable throws a ReferenceError because it's not initialized yet, even though it's hoisted.
You can define a variable inside a function and return another function that accesses it. The inner function forms a closure, keeping the variable private and only accessible through the returned function.
Objects in JavaScript inherit properties and methods from their prototype. When you access a property, JavaScript looks up the prototype chain until it finds it or reaches the end. ES6 classes use prototypes under the hood for inheritance.
ESM uses import and export syntax, supports static analysis, and is natively supported in browsers and modern Node.js. CommonJS uses require and module.exports, is synchronous, and is standard in older Node.js. ESM is preferred for modern projects.
I use try/catch blocks inside async functions to handle errors. Alternatively, I can attach a .catch handler to the returned promise. This ensures errors are caught and handled gracefully.
Microtasks include promise callbacks and MutationObservers, and are executed after the current call stack but before the next macrotask. Macrotasks include setTimeout, setInterval, and I/O, and are handled after microtasks. This affects the order of async code execution.
Immutability prevents unexpected side effects by avoiding mutation of data. Methods like map, filter, and reduce return new arrays, making it easier to write predictable and maintainable code.
JavaScript converts values to a common type when using ==. For example, 0 == false is true because both are coerced to 0. This can lead to bugs, so I prefer === for strict comparisons.
this binding caused a production bug. How did you fix it?