Mistake: Comparing NaN
Find the Mistake Medium
Question
What is wrong with this assertion?
Code
test('NaN check', () => {
expect(NaN).toBe(NaN);
});
Hint
NaN is never equal to itself in JavaScript.
Explanation
The mistake is using toBe to compare NaN. In JavaScript, NaN === NaN is false (NaN is the only value not equal to itself). The test will fail. The fix: use toBeNaN: expect(NaN).toBeNaN();, which correctly checks for NaN values.
Related Resources
Related Lessons
- Jest Basics
Getting started with Jest.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.