Notes · JavaScript
JavaScript fundamentals
Declarations, types, equality, modules
On this page · 4 sections
Overview
Language basics that come up in screening rounds. Scope, hoisting, this, and prototypes are in js-internals.md. The event loop is in js-async-event-loop.md.
Key concepts
-
var,let,const:varletconstScope Function Block Block Before its line undefinedReferenceErrorReferenceErrorReassign Yes Yes No Redeclare Yes No No -
constobjects:conststops reassignment, not changes.const arr = []; arr.push(1);works. -
Types: Seven primitives:
string,number,bigint,boolean,undefined,null,symbol. They are copied and compared by value. Everything else (objects, arrays, functions) is an object, compared by reference. -
typeofquirks:typeof nullis'object'. UseArray.isArrayfor arrays. -
==vs===:===compares without converting types.==converts first.0 == false; // true 0 === false; // false null == undefined; // true NaN === NaN; // false, use Number.isNaN(x) -
Falsy values:
false,0,-0,0n,'',null,undefined,NaN. Everything else is truthy, including'0',[],{}. -
||vs??:a || busesbfor any falsya.a ?? busesbonly whenaisnullorundefined. So0 || 5is5, but0 ?? 5is0. -
Optional chaining:
user.address?.citygivesundefinedinstead of throwing whenaddressisnull. -
Destructuring, spread, rest:
const { name, ...rest } = { name: 'Alice', age: 30 }; // rest = { age: 30 } const [x, , z = 0] = [1, 2]; // x = 1, z = 0 const more = [...[1, 2, 3], 4]; // [1, 2, 3, 4] function log(...args) { return args; } // args is an array -
Function forms: Declarations (hoisted), expressions, arrow functions, and IIFEs:
(function () { ... })(), a function that runs right away. IIFEs were the old way to get a private scope before modules. -
Shallow copy:
{ ...obj }or[...arr]. Nested objects are still shared. -
Deep copy:
structuredClone(obj). HandlesDate,Map, and cycles; throws on functions.JSON.parse(JSON.stringify(obj))losesundefined, functions, andDatetypes, and fails on cycles. -
Map/Set:Maptakes any key and keeps insertion order.Setkeeps unique values:[...new Set(arr)]removes duplicates. -
WeakMap/WeakSet: Keys are objects and do not keep them alive. Good for data attached to objects you do not own. -
Array methods:
map,filter,reducereturn new values.sort,reverse,splicechange the array;toSorted,toReversed,toSplicedreturn copies. -
Default sort: Compares as strings.
[10, 2, 1].sort()gives[1, 10, 2]. Usesort((a, b) => a - b). -
Classes: Syntax on top of prototypes. Support
#privatefields,static,extends/super. Always strict mode. -
ES modules vs CommonJS: ES modules (
import/export) are static and strict. CommonJS (require/module.exports) is Node’s older, synchronous system.
Interview focus
[] == falseistrue, but[]is truthy:==turns both into numbers ([]→''→0). Truthiness is a separate rule.nullvsundefined:undefinedmeans not set yet.nullmeans set to empty on purpose.- Deep copy:
structuredClone. Explain what JSON round-tripping loses. - When to use
WeakMap: Data per object that should go away with the object.
Common mistakes
- Thinking
constmeans immutable: It only blocks reassignment. ||for defaults when0or''are valid: Use??.- Changing nested data after a shallow copy: The original changes too.
sort()on numbers without a comparator: Sorts as strings.
From the notes repository on GitHub, synced Nov 17, 2026.Source ↗ (opens in a new tab)Report a mistake ↗ (opens in a new tab)