JavaScript skill practice
How do you approach this?
Find JavaScript Logic Flaws Before Shipping
Published · Updated
Published by Unrust. Scoring and editorial method
Quick answer
Find a JavaScript flaw by translating the function's expected behavior into concrete ordinary and boundary cases, then tracing those cases through each condition. Truthiness is a frequent trap because zero, an empty string, false, and null-like values do not mean the same thing to a product rule. A clean-looking conditional deserves the same contract check as a longer implementation.
Original example
Do not use truthiness to decide whether an index exists
function selectAt(items, index) {
if (index) return items[index];
return null;
}This original snippet is useful because index zero is a valid position while JavaScript treats it as falsy. The point is not to memorize a replacement, but to write the selection contract first: can the caller omit an index, can it be negative, and should an out-of-range index produce a fallback or an explicit failure?
Test the exact boundary, not a nearby happy path
If a function accepts an index, count, flag, or optional field, exercise the values that change the branch: zero, an empty string, false, undefined, and the first value just past a limit. These cases reveal whether the code asks the question the requirement actually asks.
Read operators in context
Optional chaining, nullish coalescing, equality, and logical operators are concise but have distinct rules. Do not label an expression wrong because it is unfamiliar; instead, evaluate it with a small input table and compare the result with the caller's documented expectation.
Check a concrete outcome
Use the original example with this input. These are local teaching fixtures, checked against the displayed snippet; they do not call external services.
Contract: a missing index returns null; a valid zero should return first. The shown truthy check returns null for both, exposing a real failure. Use an explicit undefined check and validate the integer range before indexing.
| Expression after the example | Observed result |
|---|---|
| [selectAt(["first", "second"], 0), selectAt(["first", "second"], undefined)] | [null, null] |
Checklist
- Which values make this JavaScript condition take the other branch?
- Does zero have a valid domain meaning distinct from absence?
- What should the caller observe for an out-of-range input?
Practice loop
- 01Write one ordinary input and two JavaScript boundary inputs for the condition you are reviewing.
- 02Record how the branch treats zero, an empty string, false, and an absent value when each is relevant.
- 03Add a focused test for the first input where code behavior and the stated contract differ.
Limits of this page
Small condition checks do not cover every JavaScript risk, including cross-browser behavior, prototype interactions, or framework-specific serialization. A one-off exercise cannot explain whether a missed branch came from an unfamiliar requirement, time pressure, or a skill that needs more practice.
Source context
Published by Unrust as practical teaching material using public language documentation and original examples. See the scoring and editorial method for scope, checking and limitations.
Check your own starting point
One diagnostic session is a snapshot, not causal proof of why a result occurred. It can help you choose a focused next practice step without making a broader claim about your ability.
Take the free diagnosticTake the read in JavaScript →Same six skills, side by side on your dashboard.