Skip to content

Instantly share code, notes, and snippets.

@muthhukumar
Created August 3, 2021 04:06
Show Gist options
  • Save muthhukumar/514907b71cf210c3f8f6f3e7cf6f8a24 to your computer and use it in GitHub Desktop.
Save muthhukumar/514907b71cf210c3f8f6f3e7cf6f8a24 to your computer and use it in GitHub Desktop.
custom function that behaves as ===
function strictEqual(a, b) {
if ((Object.is(a, 0) && Object.is(b, -0)) || (Object.is(a, -0) && Object.is(b, 0))) {
return true
}
if (Object.is(a, NaN) && Object.is(b, NaN)) {
return false
}
return Object.is(a, b)
}
console.log(strictEqual(10, 10) === true)
console.log(strictEqual(undefined, undefined) === true)
console.log(strictEqual(null, null) === true)
console.log(strictEqual('10', '10') === true)
console.log(strictEqual({}, {}) === false)
console.log(
strictEqual(
function () {},
function () {},
) === false,
)
console.log(strictEqual(Symbol(), Symbol()) === false)
console.log(strictEqual(1n, 1n) === true)
console.log(strictEqual(NaN, NaN) === false)
console.log(strictEqual(-0, 0) === true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment