0

Based on the definition of object that doesn't guarantee properties order, are there chances that the following will return false?

JSON.stringify({a: "A", b: "B"}) == JSON.stringify({a: "A", b: "B"})

UPDATE: of course I tried it in the console and I always get true but that goes against the fact the order of the properties is not guaranteed. I'm not sure I understand at what point the order of the properties is not the the same as the one at declaration time.

Also the following proved that the order of the attribute in maintained:

example = {}
example.a = "A"
example.c = "C"
example.b = "B"

JSON.stringify(example) == JSON.stringify({a: "A", c: "C", b: "B"})
Francesco Meli
  • 1,985
  • 1
  • 16
  • 36

1 Answers1

0

JSON.stringify() returns a string so if the order of the attributes changes the returned string changes.

just try it.

console.log(JSON.stringify({a: "A", b: "B"}) == JSON.stringify({a: "A", b: "B"}));
console.log(JSON.stringify({a: "A", b: "B"}) == JSON.stringify({b: "B", a: "A"}));
JSmith
  • 3,635
  • 3
  • 24
  • 36