Don’t forget about the order of your keys
=== would behave as they should, but to your surprise, they don’t.
Because objects are reference types, so you can’t use your normal equality operators, i.e.
How do you check objects for equality then? There are a couple of approaches you can take (check at the end of the article), but this article is focused on why you shouldn’t use
JSON.stringify seems to be the most obvious and easiest choice for comparison as no external dependency is required.
But wait! It’s not as simple as it seems, as the name suggests this method converts objects to strings first and comparison takes place afterward.
So, if the objects to be compared have properties entered in the same order, comparison will work just fine, but in the latter case, where the order has changed, the equality fails.
Although it works without installing libraries or packages from npm,
JSON.stringify certainly isn’t the best option to compare objects.