All sample questions / JavaScript

What does console.log({ a: 1 } === { a: 1 }) print? JavaScript Objects, Destructuring and Immutability

Block 5, Objects, Destructuring and Immutability, question 6 of the JavaScript sample

console.log({ a: 1 } === { a: 1 });

What does this print?

  1. A. undefined
  2. B. TypeError
  3. C. false
  4. D. true
Show the answer and why each option is right or wrong

Answer, C. false

A. undefined
Assumes the comparison silently fails to produce a boolean when neither side is a named variable.
B. TypeError
Assumes comparing two freshly created object literals directly is not valid syntax.
C. false
Correct. Strict equality on two objects compares references, and these are two distinct object literals, so the comparison is false.
D. true
Assumes two object literals with identical contents are considered equal by strict equality.

Strict equality between two objects never inspects their properties, it only checks whether both sides refer to the exact same object in memory. Two separate object literals, even with identical shape and values, are always two different objects.

The answer above is what the Node interpreter printed when the snippet was run, not what someone expected it to do.

This question comes from a bank of 600 JavaScript interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 600 JavaScript questions, 9 euros

Or play the free JavaScript sample, twenty questions in the browser, also as a free PDF.

Previous question Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.