Irbisa · Cheatsheet
JavaScript for React
Closures, scope, this, prototypes, equality, the event loop, promises and async/await, modules, immutability, array methods
What is the difference between
var,letandconst, and what is the temporal dead zone?function demo() { console.log(a); // undefined: var is hoisted and initialized // console.log(b); // ReferenceError: b is in the temporal dead zone var a = 1; let b = 2;…What is the difference between
==and===in JavaScript?What is the difference between primitive and reference types in JavaScript?
What is the difference between
nullandundefined?What is a closure, and where do closures show up in React?
function createCounter() { let count = 0; // captured by the closures below return { increment: () => ++count, get: () => count, };…What does this code log, and how do you make it log 0, 1, 2?
for (var i = 0; i < 3; i++) { setTimeout(() => { console.log(i); }, 0);}What does this code log, and why can a function declaration be called before its line while a function expression cannot?
console.log(greet());console.log(typeof hello); function greet() { return 'hi';} var hello = function () { return 'hello';}; console.log(hello());console.log(wave()); var wave = () => 'wave';What do destructuring, spread and rest, optional chaining
?.and nullish coalescing??do?const settings = { volume: 0, theme: null, user: { name: 'Ada' } }; const { volume, theme = 'light', ...others } = settings;console.log(volume, theme); // 0 null: the default applies only to undefinedconsole.log(others); // { user: { name: 'Ada' } }…Which array methods do you use most in React code, and which of them mutate the array?
const cart = [ { id: 1, title: 'Book', price: 12 }, { id: 2, title: 'Pen', price: 2 }, { id: 3, title: 'Lamp', price: 30 },];…What is the difference between a shallow and a deep copy, and how do you make each?
const user = { name: 'Ada', address: { city: 'London' } }; const shallow = { ...user };shallow.address.city = 'Paris';console.log(user.address.city); // Paris: the nested object is shared…How is
thisdetermined in JavaScript, and how do arrow functions differ?const user = { name: 'Ada', greet() { return `Hi, ${this.name}`; }, greetLater() {…How does prototypal inheritance work, and what does the
classkeyword add to it?class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a sound`;…What is the difference between named and default exports, and how do ES modules differ from CommonJS?
// math.jsexport const PI = 3.14159;export function area(r) { return PI * r * r;}export default function round(n) {…What is a Promise, and how does
async/awaitrelate to it?async function loadUser(id) { try { const response = await fetch(`/api/users/${id}`); if (!response.ok) throw new Error(`HTTP ${response.status}`); return await response.json(); } catch (error) {…What is the difference between
Promise.all,Promise.allSettled,Promise.raceandPromise.any, and when do you use each?const ok = (value, ms) => new Promise((resolve) => setTimeout(() => resolve(value), ms));const fail = (reason, ms) => new Promise((_, reject) => setTimeout(() => reject(new Error(reason)), ms)); const results = await Promise.allSettled([ok('profile', 50), fail('feed down', 10)]);console.log(results.map((r) => r.status)); // ['fulfilled', 'rejected']…How do you handle errors in
async/awaitcode, and what happens to a promise rejection that nobody catches?async function loadDashboard(userId) { showSpinner(); try { // Promise.all attaches a handler to both requests at once const [profile, orders] = await Promise.all([ fetchJson(`/api/users/${userId}`),…How does the JavaScript event loop work: what are the call stack, microtasks and macrotasks?
In what order does this code log, and why?
console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); (async () => { console.log('4'); await null; console.log('5');})(); console.log('6');Find the bug:
saveAlllogs "done" before any user is saved.async function saveAll(users) { users.forEach(async (user) => { await saveUser(user); }); console.log('done');}How do you implement
debounce, and when do you use it instead ofthrottle?function debounce(fn, delay) { let timerId; function debounced(...args) { clearTimeout(timerId); timerId = setTimeout(() => fn.apply(this, args), delay); }…What causes memory leaks in a single-page React app, and how do you find them?