# Find JavaScript Logic Flaws Before Shipping

> Find JavaScript logic flaws by comparing branch conditions with the contract and testing boundaries where truthiness, indexes, and optional values change behavior.

- Canonical HTML: [https://unrust.dev/languages/javascript/find-the-flaw](https://unrust.dev/languages/javascript/find-the-flaw)
- Markdown representation: [https://unrust.dev/languages/javascript/find-the-flaw/index.md](https://unrust.dev/languages/javascript/find-the-flaw/index.md)

Published: 2026-07-14

## Direct 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.

## 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.

## Original example

### Do not use truthiness to decide whether an index exists

```javascript
function selectAt(items, index) {
  if (index) return items[index];
  return items[0];
}
```

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?

## 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

1. Write one ordinary input and two JavaScript boundary inputs for the condition you are reviewing.
2. Record how the branch treats zero, an empty string, false, and an absent value when each is relevant.
3. Add 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

Editorially reviewed for Unrust using public language documentation, common code-review practice, and small original examples written for this resource system.

## Related resources

- [How to Validate Copilot Suggestions](https://unrust.dev/use-cases/validate-copilot-suggestions/index.md): Validate autocomplete and Copilot suggestions by checking their contract, assumptions, and tests before a small completion becomes a hidden behavior change.
- [How to Review AI-Generated Code Safely](https://unrust.dev/use-cases/review-ai-generated-code/index.md): Use a contract-first review loop to inspect AI-generated code, test risky boundaries, and decide whether a proposed change is ready to merge.

Next step: [Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
