Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Compare Objects With ===

In this lesson, you checked whether Monday’s meals changed between last week and next week, and found that === doesn’t compare what’s inside two arrays.

Key Points

  • After the day-by-day copy, nextWeek[0].meals and lastWeek[0].meals hold the same two strings, yet nextWeek[0].meals === lastWeek[0].meals is false. The copy built a new meals array, so there are two arrows to two arrays.
  • Between two arrays or objects, === asks one question: do these two arrows point at the same object? Comparing which object two arrows reach is called comparing by identity.
  • Give one array a second name, as const mondayMeals = lastWeek[0].meals; does, and mondayMeals === lastWeek[0].meals is true: two arrows, one array.
  • == asks the same question between two arrays, so nextWeek[0].meals == lastWeek[0].meals is false too.
  • For arrays and objects, .includes() checks each slot with the same question. lastWeek.includes(nextWeek[0]) is false: next Monday looks exactly like last Monday, but the copy made it a new record. lastWeek.includes(lastWeek[0]) is true, because that slot holds an arrow to the real Monday.
  • Strings and numbers compare by value: last Monday’s dinner and a separately typed "jollof rice" are === equal, so lastWeek[0].meals[1] === "jollof rice" is true.
  • The rule: between arrays or objects, === asks whether two arrows reach the same object. It never looks at the contents.

Final Code

The final plan.js runs every check from the lesson:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the exercise, you wrote planner helpers that leave last week as it was. Now next week’s plan is taking shape, and the Okafors want the planner to tell them which days changed.

00:12 Here’s plan.js. Last week’s seven day records sit at the top, folded into one line here to save space. Next week is a copy of every day, so each day gets a meals list of its own.

00:24 Then Chidi’s tweak puts egusi soup on Tuesday. Before the planner checks every day, try the check on Monday, which nobody touched. The first two new lines log Monday’s meals from each week.

00:37 The last line uses === to ask whether they’re the same. Your turn: pause the video and predict what the last line prints.

00:51 It prints false. Run the file, and next Monday and last Monday both show leftover rice and jollof rice. They hold the same lunch and dinner in the same order.

01:02 Yet === says they’re not the same.

01:06 So a planner built on this check would call Monday changed, though nobody touched it.

01:13 To see why, draw the arrows. Last Monday’s record has a meals property, and its arrow points at an array with Monday’s lunch and dinner. The copy line built a new meals array for next Monday, with the same two strings.

01:27 Next Monday’s meals arrow points at that new array.

01:31 Between two arrays, === doesn’t look inside them. It asks one question: do these two arrows point at the same array? Here they point at two arrays, so the answer is false.

01:44 Comparing which object two arrows reach is called comparing by identity. You can confirm this with one array under two names. A new name, mondayMeals, points at last Monday’s meals array, the same one.

01:58 Now === compares two arrows to one array, and it prints true. Watch out if you switch to == instead. Between two arrays, it asks the same question, so it prints false too.

02:13 The same question hides inside .includes(), the array method that tells you whether an array holds an item. Is next Monday one of last week’s days?

02:23 Ask last week whether it includes next Monday, a record that looks exactly like last Monday.

02:29 It prints false. For objects, .includes() checks each slot with the same question as ===: same arrow? The copy made next Monday a new record, so no slot points at it.

02:42 Pass it last week’s first slot instead, and it prints true. That slot holds an arrow to the real Monday.

02:50 Strings work differently. Compare last Monday’s dinner with jollof rice typed fresh as a new string, and === prints true. Strings and numbers compare by value.

03:02 So here’s the rule. Between arrays or objects, === asks whether two arrows reach the same object. It never looks at the contents.

03:13 You’ve seen two equal meal lists fail that check, one list with two names pass it, and .includes() miss a look-alike record. Later in this section, in Exercise: Find and Unpack Meals, you’ll write a check that compares the meals themselves.

03:28 First, in the next lesson, Unpack Arrays and Objects, you’ll pull values out of a day record.

Become a Member to join the conversation.