Variables & scope
01
Prefer const; use let when reassignment
is required. Avoid var in modern code.
const name = 'Ada'; let score = 0; { let local = true; }
The essential concepts, patterns, and gotchas to review before a front-end technical interview.
Prefer const; use let when reassignment
is required. Avoid var in modern code.
const name = 'Ada'; let score = 0; { let local = true; }
Declarations are fully hoisted. Arrow functions have lexical
this and cannot be constructors.
function add(a, b) { return a + b; } const double = n => n * 2;
Use destructuring and non-mutating array methods to keep transformations clear.
const { id, title } = post; const active = users .filter(u => u.active) .map(u => u.name);
Select elements with CSS selectors and attach behavior with event listeners.
const button = document.querySelector('#save'); button.addEventListener('click', save);
async functions return promises.
await pauses only that function—not the thread.
async function loadUser(id) { const res = await fetch(`/users/${id}`); return res.json(); }
Throw meaningful errors near the cause; catch them where recovery or reporting is possible.
try { await submit(); } catch (error) { console.error(error.message); }
A function remembers variables from the scope where it was created.
function counter() { let n = 0; return () => ++n; }
Use strict equality by default. Know the six falsy primitives:
false, 0, '',
null, undefined, and NaN.
0 === '0' // false Boolean([]) // true
Template literals, spread, optional chaining, and nullish coalescing reduce boilerplate.
const label = `${user?.name ?? 'Guest'}`; const next = { ...state, ready: true };
For maximum product of three, compare the three largest values with the largest value times the two smallest.
a.sort((x,y) => x-y); return Math.max( a[0]*a[1]*a.at(-1), a.at(-3)*a.at(-2)*a.at(-1) );