← All posts
JavaScript60 min total · 16 parts
JavaScript Array and Object Methods Cheat Sheet
Part 16 of 16 · ~2 min
Common Mistakes Worth Remembering
- Treating
.sort()and.reverse()as if they were as harmless as.map()or.filter()— the first two rewrite your array right where it sits; the second two never touch it. - Sorting a list of numbers with a bare
.sort()and being surprised the result isn't in numeric order — it was never going to be, without a comparator telling it what to compare on. - Removing a candidate with
delete candidates[i]instead of.splice(), which leaves a hole rather than actually shrinking the array —.lengthstays the same, and.forEach()/.map()quietly skip straight over the gap. - Reaching for
.forEach()when you actually need something back, or trying to stop it partway through withbreak— it returns nothing and can't be interrupted;.map(),.reduce(), or a plainfor...ofloop can do either. - Skipping the initial value on a
.reduce()call whose array could plausibly be empty someday — harmless for months, then aTypeErroron the first day it actually is. - Reaching for
||to supply a fallback when0,"", orfalsemight be a genuine, intentional value rather than a missing one —??is almost always what belongs there instead. - Treating spread or
Object.assign()as if they cloned everything — they only duplicate the outer layer, so editing a nested field afterward changes the "original" too, not just your supposed copy. - Expecting
===to tell you whether two objects hold the same data — it only ever answers whether they're the same object, which is a different question. - Reaching for a plain object on a problem that's genuinely
Map-shaped — non-string keys, constant adding and removing, actually needing to trust.size— and then losing an afternoon to why a numeric-looking key won't match.
If any of this felt familiar from a different angle, that's not a coincidence — useState and useReducer, covered in React Fundamentals, lean on this exact same set of methods, and the reason React insists on a brand-new array or object every time state changes traces straight back to the very first section above. The scope rules, closures, and event-loop timing that all of this code actually runs inside of get their own full treatment in JavaScript Core Concepts.
A reasonable next step: open the code lab and run a few of these against a dataset you put together yourself, not just the candidates above.
Practice this
UI Challenges
Continue learning
- JavaScriptJavaScript Core Concepts: Scope, Closures, `this`, and the Event Loop
- Interview & Career PrepThe Non-Technical Half of the Interview: Behavioral Questions, the STAR Method, and What Recruiters Are Actually Scoring
- AI & LLM EngineeringAI & LLM Engineering Fundamentals: Prompting, RAG, Embeddings, and Function Calling