Curriculum
Concept 16 // Module 02: Values, Operators & Control Flow
Equality: ==, === & Object.is()
Module 02: Values, Operators & Control Flow•Concept 16 of 100• 5 min read
Equality: ==, === & Object.is()
JavaScript provides three equality testing operations: loose equality (`==`), strict equality (`===`), and `Object.is()`. Strict equality tests value and type without coercion, while `Object.is()` additionally distinguishes `-0` from `+0` and treats `NaN` as equal to `NaN`.
The Core Concept
Always default to `===` in daily development. Use `Object.is()` when you must accurately detect `NaN` state changes or distinguish signed zeroes in graphics/physics simulations.
Syntax Reference
// Loose equality (coerces types)
a == b;
// Strict equality (no coercion)
a === b;
// SameValue equality (exact IEEE-754 semantics)
Object.is(a, b);Live Interactive Studio
`==` follows complex coercion algorithms (ToNumber, ToPrimitive). `===` is almost always preferred for predictable behavior.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Strict vs Loose Equality
console.log(5 == "5"); // true (coerces string to number)
console.log(5 === "5"); // false (different types)
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log([] == 0); // true (coerces [] to "" then to 0!)
console.log([] === 0); // false`==` follows complex coercion algorithms (ToNumber, ToPrimitive). `===` is almost always preferred for predictable behavior.
02
Object.is Corner Cases (NaN and -0)
console.log(NaN === NaN); // false! (IEEE-754 rule)
console.log(Object.is(NaN, NaN)); // true!
console.log(+0 === -0); // true!
console.log(Object.is(+0, -0)); // false!
console.log(1 / +0); // Infinity
console.log(1 / -0); // -InfinityIEEE-754 specifies `NaN !== NaN`. `Object.is` implements the ECMAScript SameValue algorithm, making it ideal for state reconciliation in frameworks like React.
03
1. Core Usage: Equality
// Core syntax and usage for Equality: ==, === & Object.is()
const feature = 'equality-comparisons-object-is';
console.log('Concept 16:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Equality: ==, === & Object.is().
04
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('equality-comparisons-object-is'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Equality: ==, === & Object.is().
05
3. Clean Architecture & Production Pattern
// Production-grade pattern for Equality: ==, === & Object.is()
const settings = Object.freeze({
id: 'equality-comparisons-object-is',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
React State Diffing: React's `useState` and `memo` rely internally on `Object.is` to avoid unnecessary re-renders.
Input Sanitization: Preventing silent bugs caused by `'0' == false` or `'' == 0`.
Mathematical Algorithms: Preserving directional signs of zero in vector calculations.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
Strict equality checks both the type tag bits and the value payload. For heap objects, it simply compares the 64-bit memory addresses of the two object pointers.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Equality: ==, === & Object.is() in JavaScript?
// Concept 16: Equality: ==, === & Object.is()
// Understanding the core mental model1 / 10