toBe vs toEqual
Question
What is the difference between toBe and toEqual?
Hint
Use toBe for primitives, toEqual for objects/arrays.
Explanation
toBe uses Object.is (similar to ===) — checks reference equality. For primitives (numbers, strings, booleans), toBe works. For objects/arrays, toBe checks if they are the same reference, not the same content. toEqual performs deep recursive equality — {a: 1} toEqual {a: 1} passes, but {a: 1} toBe {a: 1} fails (different references). Use toBe for primitives and null/undefined. Use toEqual for objects and arrays. toStrictEqual is stricter (checks type and structure).
Related Resources
Related Lessons
- Assertions
Learn assertions.
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.