Edge Case
Testing
Learn to systematically identify and test boundary conditions in JavaScript: the inputs your code is most likely to break on.
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
The Boundary Spectrum
Categories of Edge Cases
"", [], {}, null, undefined0, -1, Number.MAX_SAFE_INTEGER, single-element arrays"5" vs 5, NaN, Infinity, boolean-like strings\n, \t, or multi-byte charactersThe ZOMBIES Rule
A systematic mnemonic for remembering which edge cases to test for any function:
0, empty strings, empty arrays?1null, undefined, NaN, InfinityEdge 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
console.assert() or a test framework.Example 1: findMax
Testing findMax Edges
findMax([]) returns undefined: no crash, but is that intentional?findMax([42]) returns 42. Works: the loop body never executes.findMax([-Infinity, 0]) returns 0. Negative numbers must be handled.findMax([1, NaN, 3]) returns 3 because comparisons with NaN are always false, so NaN is silently skipped. Is that correct?Example 2: divideNumbers
Simple division. Let's apply ZOMBIES systematically:
divideNumbers(0, 5) \(\rightarrow\) 0 ✓divideNumbers(5, 0) \(\rightarrow\) Infinity ✕ No error thrown!divideNumbers(0, 0) \(\rightarrow\) NaN ✕ Corner case: both at zero!divideNumbers("10", 2) \(\rightarrow\) 5 ★ Type coercion: works by accidentA Robust Version
Example 3: reverseString
This classic approach has a hidden edge case. Walk through the ZOMBIES:
"hello" \(\rightarrow\) "olleh""" \(\rightarrow\) """a" \(\rightarrow\) "a"split("") splits surrogate pairs!Fixing with Spread Syntax
Common Mistakes
[1,2,3] but never for [], [1], or null. Most bugs hide in the edges."5" + 1 gives 6. It gives "51". Always validate input types explicitly.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?
NaN stands for "Not a Number," but paradoxically, typeof NaN evaluates to "number". This is a classic JavaScript edge case: always use Number.isNaN() to detect it.Question 2 of 5
A function takes an array and returns its first element. Which edge case is most critical to test?
[][0] returns undefined, which could silently propagate through your program. The other options are normal inputs that most implementations handle correctly.Question 3 of 5
What is the difference between an edge case and a corner case?
0 by 0 is a corner case because both the numerator and denominator are at their boundary values simultaneously.Question 4 of 5
Consider this function:
function isAdult(age) { return age >= 18; }
You need to write boundary tests for this function. The valid age range is \(0\) to \(150\).
Which set of test values best covers the boundary conditions?
18 (the decision point), so we test 17, 18, and 19. We also test the range limits: -1, 0 (lower) and 150, 151 (upper).Question 5 of 5
What does "5" + 1 evaluate to in JavaScript, and why is this an edge case concern?
+ is used with a string and a number, JavaScript coerces the number to a string and concatenates. This is a critical edge case: if user input arrives as a string (e.g., from a form), your math operations silently produce wrong results instead of errors. Always validate types.