Object Equality

Hi all,

I’m doing exercise create Equality related to Objects in JavaScript course.

The exercise is to create two objects with the same properties using a factory function:

function CreateAddress(street, city, zipCode){
  this.street = street;
  this.city = city;
  this.zipCode = zipCode;
}

const address1 = new CreateAddress('a', 'b', 2477798);
const address2 = new CreateAddress('a', 'b', 2477798);

and then to create a function to check if the two objects are referecing the same address in memory.

function areSame(addressObj1, addressObj2) {
  return addressObj1 === addressObj2
}

//output: false

my question is more about the function syntax than the concept of the exercise.
If I use:

function areSame(addressObj1, addressObj2) {
if (addressObj1 === addressObj2)
return;

//output: underfined

What is going wrong on the second syntax? Thanks

With

your function returns a boolean result (true or false).

With

your function returns nothing, the result of it is undefined.

If you for what ever reason want to use an if you need to explicitly return true or false:

function areSame(addressObj1, addressObj2) {
if (addressObj1 === addressObj2)
   return true;

return false;
2 Likes

@SAM @YuriAmancio

LOL

My previous answer was so out of context.

To answer your question, I am not sure if using equality operator is a good technique to compare two memory address.
Since memory address is about the execution environment, I am not sure if the language itself supports such feature (getting memory address of the variable).

@maverick I’m sure you have good intentions but

1 Like

The exercise has another function to compare if the properties in these two objects are the same but in this case I wanted to clarify the same address in memory. Thanks for the tips.