# Question on JavaScript Object Equality Exercise

**URL:** <https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390>\
**Category:** JavaScript\
**Created:** [December 22, 2020, 6:29pm UTC](https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390 "2020-12-22T18:29:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cursedhive](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@cursedhive](https://forum.codewithmosh.com/u/cursedhive)\
**Post date:** [December 22, 2020, 6:29pm UTC](https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390/1 "2020-12-22T18:29:14Z")

</div>

I have a question about video “17- Exercise 3- Object Equality” in " The Ultimate JavaScript Mastery Series - Part 1" course.

Mosh’s textbook answer for the areEqual function is:

```
let address1 = new Address('a', 'b', 'c');
let address2 = new Address('a', 'b', 'c');

function areEqual(address1, address2) {
	return address1.street === address2.street &&
	address1.city === address2.street && 
	address1.zipCode === address2.zipCode;
}

```

In contrast, I wrote:

```
function areEqual(address1, address2) {
    for (let key in address1)
        if (address1[key] !== address2[key])
            return false;

    return true;
}

```

My code appears to work, and seems cleaner and more extensible than Mosh’s answer. Is there anything wrong with my solution that I should consider?

Thanks!

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [December 23, 2020, 6:11am UTC](https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390/2 "2020-12-23T06:11:45Z")

</div>

I’d say your solution is fine for comparing two `Address` objects. Note that your solution would also compare functions if our Address object had any. And think of what would happen if we passed in an empty object or a number for `address1`. The function would return `true`.

---

<div class="post-metadata">

**Author:** ![cursedhive](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@cursedhive](https://forum.codewithmosh.com/u/cursedhive)\
**Post date:** [December 24, 2020, 12:39am UTC](https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390/3 "2020-12-24T00:39:22Z")

</div>

Thanks, that’s a good reminder to consider the edge cases!

---

<div class="post-metadata">

**Author:** ![Wirkoo](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Wirkoo](https://forum.codewithmosh.com/u/Wirkoo)\
**Post date:** [December 28, 2020, 10:30am UTC](https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390/4 "2020-12-28T10:30:22Z")

</div>

> [@cursedhive](#):
>
> th my solution that I

I also used the same for in loop and this is better approach from avoiding repitition of 3 && operators.

---

<div class="post-metadata">

**Author:** ![Wirkoo](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@Wirkoo](https://forum.codewithmosh.com/u/Wirkoo)\
**Post date:** [December 28, 2020, 10:33am UTC](https://forum.codewithmosh.com/t/question-on-javascript-object-equality-exercise/1390/5 "2020-12-28T10:33:53Z")

</div>

i think we can use the if statement before applying for in loop in these conditions, because with for in loop there is also benefit of testing if we dont know the name of object properties i.e. street, city and zipcode.
