Question and potential spoiler | Equality of Objects

So the challenge itself is not that hard and I am able to pass it,
but (and here comes the potential spoiler):

apparently { value : 1 } == { value : 1 } is false?
Why is that?

Link to the challenge:

Hello,

β€œ==” makes a loose equality check while β€œ===” makes a strict equality check.

Here is an article about the differences between the two:

https://medium.com/jspoint/loose-vs-strict-equality-in-javascript-3e5136720b00#:~:text=The%20Loose%20Equality%20or%20Abstract,constraints%20imposed%20by%20Strict%20Equality.

1 Like

Hello there,

This is a common pitfall developers find, especially when working with a high-level language like JavaScript.

It all boils down to the references of the object literals:

const objectA = { value: 1 };
const objectB = { value: 1 };

console.log(objectA === objectA); // returns true - they point to the same reference object in memory
console.log(objectA === objectB); // returns false - different objects in memory

Now, remember, all I have done is given the two anonymous object literals you created, variable names. Otherwise, it is the same distinction.

Hope this helps

1 Like

Thanks it does help a lot!
As I understand it, this is a general rule for not only objects, but also arrays and functions, right?

Thanks!

  • Non-primitive values such as Array , Object , or Functions aren’t equal to each other except themselves.

This (from the article) helped a lot!

1 Like

Ah, but that is why I specifically said object literals. In JS, Arrays, Strings, Functions, Dates are all objects. The thing created with a curly-brace (e.g. { value: 1 }) is an object literal. In Python, it is called a dictionary. In other languages it is called a mixture of enum, struct…

Point is, JavaScript is heavily object-based (although, not typically considered an object oriented programming language, and I am not sure why).

Hope that clarifies.

1 Like

Oh yes sure, I forgot about that. Makes perfect sense now

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.