JavaScript foundations for coding rounds · core
JavaScript gotchas in coding rounds
In JavaScript, sort() compares strings unless you pass a comparator, all numbers are 64-bit floats with no integer type, Map keeps insertion order, and deep recursion throws RangeError. So write numeric comparators, use Math.trunc or BigInt where needed, and switch to an explicit stack for deep inputs.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- sort() with no comparator converts values to strings and compares UTF-16 code units: [10, 9, 1, 2].sort() gives [1, 10, 2, 9]. Use (a, b) => a - b. sort() mutates and is stable since ES2019; toSorted() returns a copy.
- A comparator must return a number (negative, zero, positive). (a, b) => a > b returns a boolean and gives engine-dependent, wrong results.
- Numbers are IEEE-754 doubles: integers are exact only up to Number.MAX_SAFE_INTEGER (2^53 − 1); 0.1 + 0.2 !== 0.3. Integer division: Math.trunc(a / b) rounds toward zero, Math.floor rounds down (different for negatives). % keeps the sign of the left side: -7 % 3 is -1. Use BigInt for values above 2^53.
- Map iterates in insertion order; setting an existing key does not move it (delete then set to move it, the LRU trick). Keys use SameValueZero: objects by reference, NaN equals NaN. Plain objects put integer-like keys first in ascending order.
- Recursion depth is limited (about 10,000 frames in Node, it varies). Deep trees, long linked lists or 1000×1000 grids need an explicit stack or queue.
- There is no built-in heap, priority queue, deque or sorted map. A queue made with shift() is O(n) per dequeue; use a head index.
- Array(n).fill([]) puts the SAME array in every slot. Use Array.from({ length: n }, () => []).
- str.length counts UTF-16 units, so an emoji counts as 2; [...str] splits by code points. Strings are immutable: build with an array and join, or +=.
The red pen
Common traps: mistake → fix
Mistake: sort() sorts numbers numerically
Fix: it sorts them as strings unless you pass a comparator.
Mistake: (a, b) => a > b is a fine comparator
Fix: it returns true/false; V8 treats false as 'equal' and the result is wrong.
Mistake: JavaScript has integers
Fix: it has only doubles (and BigInt); 2 ** 53 + 1 prints 9007199254740992.
Mistake: Object keys keep insertion order
Fix: integer-like keys are moved first, in numeric order; use Map when order matters.
Mistake: Array(3).fill(Array(3).fill(0)) makes a 3×3 grid
Fix: all rows are the same array, so one write changes every row.
Easy to hard
Questions interviewers ask, easy to hard
- What does [10, 9, 1, 2].sort() return and why?
- How do you do integer division in JavaScript? What about negative numbers?
- Why does your recursive DFS crash on a 1000×1000 grid, and how do you fix it?
- Does a Map keep order? How would you move a key to the end?
- You need a priority queue in JavaScript. What do you do?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.