01 / 21

Edge Case
Testing

Learn to systematically identify and test boundary conditions in JavaScript: the inputs your code is most likely to break on.

JavaScript

What Are Edge Cases?

An edge case is an input or situation that occurs at the extreme boundaries of expected operating conditions. They represent the inputs your function barely handles: the emptiest string, the largest number, the unexpected null.

Why Edge Cases Matter

✕ Bugs Hide Here
Most production bugs live at boundaries: off-by-one errors, empty inputs, and type coercion surprises.
✓ Confidence
Testing edges means you can trust your code handles all valid inputs, not just the "happy path."
★ Professionalism
Senior engineers evaluate code quality by how well it handles edge cases, not just whether it works for typical inputs.

The Boundary Spectrum

Invalid Edge Normal Edge Invalid -1 0 5 100 101 For a function accepting ages 0 to 100

Categories of Edge Cases

Empty Inputs
"", [], {}, null, undefined
Boundary Values
0, -1, Number.MAX_SAFE_INTEGER, single-element arrays
Type Coercion
"5" vs 5, NaN, Infinity, boolean-like strings
Special Characters
Unicode, whitespace-only strings, strings with \n, \t, or multi-byte characters

The ZOMBIES Rule

A systematic mnemonic for remembering which edge cases to test for any function:

Z
Zero: What happens with 0, empty strings, empty arrays?
O
One: Single element, single character, the value 1
M
Many: Large collections, very long strings, deeply nested objects
B
Boundaries: Min/max values, first/last index, \( \pm 1 \) of limits
I
Interfaces: Wrong types, missing properties, extra arguments
E
Exceptions: null, undefined, NaN, Infinity
S
Simple scenarios: The simplest valid case that should definitely work

Edge vs Corner Cases

Edge Case Corner Case
Definition One parameter at its extreme Multiple parameters at extremes simultaneously
Example Empty array input Empty array + null comparator
Frequency More common in practice Rarer, but more dangerous
Difficulty Easier to predict Harder: combinatorial explosion
Priority Test first Test after edges are covered

How to Systematically Test

1
Identify inputs: List every parameter your function accepts: types, ranges, and constraints.
2
Apply ZOMBIES: For each input, walk through the mnemonic: Zero, One, Many, Boundaries, Interfaces, Exceptions, Simple.
3
Write assertions: For each edge case, write a test with the expected output. Use console.assert() or a test framework.
4
Check corner cases: Combine two or more edge inputs and verify the function still behaves correctly.
5
Guard or document: Add input validation or clear error messages for invalid inputs your function cannot handle.

Example 1: findMax

function findMax(arr) { let max = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] > max) max = arr[i]; } return max; }

Testing findMax Edges

Z Zero
findMax([]) returns undefined: no crash, but is that intentional?
O One
findMax([42]) returns 42. Works: the loop body never executes.
B Boundary
findMax([-Infinity, 0]) returns 0. Negative numbers must be handled.
E Exception
findMax([1, NaN, 3]) returns 3 because comparisons with NaN are always false, so NaN is silently skipped. Is that correct?

Example 2: divideNumbers

function divideNumbers(a, b) { return a / b; }

Simple division. Let's apply ZOMBIES systematically:

Z
divideNumbers(0, 5) \(\rightarrow\) 0 ✓
Z
divideNumbers(5, 0) \(\rightarrow\) Infinity ✕ No error thrown!
E
divideNumbers(0, 0) \(\rightarrow\) NaN ✕ Corner case: both at zero!
I
divideNumbers("10", 2) \(\rightarrow\) 5 ★ Type coercion: works by accident

A Robust Version

function divideNumbers(a, b) { if (typeof a !== "number" || typeof b !== "number") { throw new TypeError("Both arguments must be numbers"); } if (Number.isNaN(a) || Number.isNaN(b)) { throw new RangeError("Arguments must not be NaN"); } if (b === 0) { throw new RangeError("Cannot divide by zero"); } return a / b; }

Example 3: reverseString

function reverseString(str) { return str.split("").reverse().join(""); }

This classic approach has a hidden edge case. Walk through the ZOMBIES:

✓ Normal
"hello" \(\rightarrow\) "olleh"
✓ Zero
"" \(\rightarrow\) ""
✓ One
"a" \(\rightarrow\) "a"
✕ Special
Multi-byte characters (like surrogate pairs) break: split("") splits surrogate pairs!

Fixing with Spread Syntax

// Broken: splits surrogate pairs "hi\uD83D\uDE00".split("").reverse().join(""); // \(\rightarrow\) garbled output (broken surrogate pair) // Fixed: spread respects code points [..."hi\uD83D\uDE00"].reverse().join(""); // \(\rightarrow\) correct reversal with intact code point

Common Mistakes

✕ Only Testing Happy Path
Writing tests for [1,2,3] but never for [], [1], or null. Most bugs hide in the edges.
✕ Trusting Type Coercion
Assuming "5" + 1 gives 6. It gives "51". Always validate input types explicitly.
✕ Forgetting NaN Comparisons
NaN === NaN is false. Use Number.isNaN() to detect it, never ===.

Edge Case Cheat Sheet

Input Type Edge Cases to Test Common Trap
Numbers 0, -0, NaN, Infinity, -Infinity typeof NaN === "number"
Strings "", " ", multi-byte chars, very long strings "" == false is true
Arrays [], [x], sparse arrays, nested arrays typeof [] === "object"
Objects {}, null, missing keys, prototype chain typeof null === "object"
Booleans Truthy/falsy coercion: 0, "", null [] == false is true

Question 1 of 5

What does typeof NaN return in JavaScript?

Question 2 of 5

A function takes an array and returns its first element. Which edge case is most critical to test?

Question 3 of 5

What is the difference between an edge case and a corner case?

Question 4 of 5

Which set of test values best covers the boundary conditions?

Question 5 of 5

What does "5" + 1 evaluate to in JavaScript, and why is this an edge case concern?