DEV Community

loading...
Cover image for Learn JavaScript - compare two objects

Learn JavaScript - compare two objects

Tizhad
・1 min read

Let's do some practice to learn JavaScript together.

  1. Write a JavaScript program to compare two objects to determine if the first one contains equivalent property values to the second one.
  • Declare two objects.
  • Create a function for comparison.
  • Inside the function define an if for comparing two first values and return related result.

In this case I need to compare the name of a object with name of b object.
To access these values we use "dot" (e.g. a.name) to access the name and compare it with the name in another object.

In the if's bracket, we should define the action and output base on our if condition. In this case, I logged a string.

  • At last you need to call function.

If you don't do this, You won't receive anything.

//objects

const a = { name: "John", family: "RD" };
const b = { name: "Jack", family: "SD" };
console.log(a.name);

function compare() {
  if (a.name === b.name) {
    console.log("They are equal");
  } else {
    console.log("They are not equal");
  }
}
compare();
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app