A utility function to compare values using loose equality, strict equality, and Object.is()
npm install equality-checkerA utility function to compare values using different equality methods in JavaScript.
``bash`
npm install equality-checker
`javascript
const check = require("equality-checker");
// Basic usage
check(5, "5");
check(0, false);
check(null, undefined);
// With custom message
check(
{ id: 1 },
{ id: 1 },
{
message: "These are different objects in memory",
}
);
`
Compares two values using three different equality methods:
- Loose equality (==)===
- Strict equality ()
- Object.is()
#### Parameters
- a - First value to compareb
- - Second value to compareoptions
- - Optional object with:message
- - Custom message to display
#### Output
The function logs the comparison results to the console with visual indicators:
- ✅ for true
- ❌ for false
`javascript
const check = require("equality-checker");
check(5, "5");
// - Comparing [5] and ["5"] -
// Loose equality: ✅ true
// Strict equality: ❌ false
// Object is: ❌ false
check(NaN, NaN);
// - Comparing [null] and [null] -
// Loose equality: ❌ false
// Strict equality: ❌ false
// Object is: ✅ true
const obj = { id: 1 };
check(obj, obj, { message: "Same object reference" });
// - Comparing [{"id":1}] and [{"id":1}] -
// Same object reference
// Loose equality: ✅ true
// Strict equality: ✅ true
// Object is: ✅ true
``