JS: Test Object Equality by JSON String, Ordering Problem

By Xah Lee. Date: . Last updated: .

If you turn object into JSON string [see JS: JSON.stringify] , then compare the string, this is not reliable because object properties is not ordered.

// comparison by JSON.stringify is not reliable

const x = { a: 1, b: 2 };
const y = { b: 2, a: 1 }; // reverse order

console.log((JSON.stringify(x) === JSON.stringify(y)) === false);

JavaScript, Object and Inheritance

JavaScript, Boolean

BUY ΣJS JavaScript in Depth