check if two objects are same in javascript

In JavaScript, there are two types of equality: strict equality and loose equality.

To check if two objects are the same, you should use strict equality (===) instead of loose equality (==).

The strict equality checks if two objects are of the same type and their contents are equal. Here's an example:

index.tsx
const obj1 = {a: 1, b: 2};
const obj2 = {a: 1, b: 2};
const obj3 = obj1;

console.log(obj1 === obj2); // false
console.log(obj1 === obj3); // true
147 chars
7 lines

In this example, obj1 and obj2 are not the same, even though their contents are equal. On the other hand, obj1 and obj3 are references to the same object, so they are equal.

Note that if you compare objects with loose equality (==), JavaScript will try to convert the objects to a common type, which can lead to unexpected behavior. It's always safer to use strict equality (===) when comparing objects.

gistlibby LogSnag