Overview
Implement objectIs(a, b) — same as Object.is(a, b). Handles NaN === NaN (true), +0 !== -0.
Examples
objectIs(NaN, NaN); // true objectIs(+0, -0); // false objectIs(1, 1); // true
Solution
Reveal solution
function objectIs(a, b) {
if (a !== a && b !== b) return true;
if (a === 0 && b === 0) return 1/a === 1/b;
return a === b;
}object-is.js
Object.is
easycodingJavaScriptComparison
Overview
Implement objectIs(a, b) — same as Object.is(a, b). Handles NaN === NaN (true), +0 !== -0.
Examples
objectIs(NaN, NaN); // true objectIs(+0, -0); // false objectIs(1, 1); // true
Solution
Reveal solution
function objectIs(a, b) {
if (a !== a && b !== b) return true;
if (a === 0 && b === 0) return 1/a === 1/b;
return a === b;
}NameTopicDifficulty
103 of 103 problems