Objects in JavaScript: References, Keys & Copies

Easy100% Free~12 mins#objects#references#object-keys#copying#basics
Interview Answer (30-Second Summary)

Objects in JavaScript are collections of key-value pairs whose keys are strings or symbols. Variables hold references to objects, so assigning or passing an object shares it and === compares references, not contents. Copy with spread or structuredClone, and use a Map when you need objects as keys.

Key Learning Objectives
✓

Create objects and read, add and delete properties.

✓

Explain why objects are compared and assigned by reference.

✓

Predict key order and know that object keys are strings (or symbols).

✓

Copy objects and split them with rest, and check for properties with in and hasOwnProperty.

The Interview Problem

What do the five lines of output say?

1const a = { x: 1 };
2const b = { x: 1 };
3const c = a;
4console.log(a === b, a === c);
5
6c.x = 2;
7console.log(a.x);
8
9const o = { b: 1, 2: 'two', a: 2, 1: 'one' };
10console.log(Object.keys(o).join(','));
11
12const key = { id: 1 };
13const map = {};
14map[key] = 'first';
15map[{ id: 2 }] = 'second';
16console.log(Object.keys(map).length, map['[object Object]']);
17
18const { x, ...rest } = { x: 1, y: 2, z: 3 };
19console.log(x, Object.keys(rest).join(''), 'y' in rest, rest.hasOwnProperty('x'));
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

false true / 2 / 1,2,b,a / 1 second / 1 yz true false

true true / 2 / 1,2,b,a / 1 second / 1 yz true false

false true / 1 / 1,2,b,a / 1 second / 1 yz true false

false true / 2 / b,2,a,1 / 2 second / 1 yz true false

V8 Engine Execution Trace
Step 1 of 5 (Line 4)

Each object literal creates a new object. c = a copies the reference, so a and c point to the same object.

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
a:#A { x: 1 }
b:#B { x: 1 }
c:#A
Console Stream
> false true

Objects in JavaScript Explained

How Objects in JavaScript Behave

Objects in JavaScript are collections of key-value pairs. Keys are strings or symbols; values can be anything, including functions (methods) and other objects.

  1. References: a variable holds a reference to an object, not the object itself. Assigning (c = a) or passing an object to a function shares the same object, and === compares references, so two objects with equal contents are not equal. Copy with { ...a } or Object.assign (shallow) or structuredClone (deep).
  2. Keys: non-symbol keys are converted to strings, so obj[{}] uses the key '[object Object]'. Use a Map when keys should be objects. Object.keys lists own integer-like keys first in ascending order, then string keys in insertion order.
  3. Checking and splitting: 'key' in obj also looks up the prototype chain, while Object.hasOwn(obj, 'key') (or hasOwnProperty) checks only the object itself. Destructuring with rest (const { x, ...rest } = obj) pulls out x and copies the remaining own properties into a new object.
Common Traps & Mistakes

Comparing two objects with === or == to check whether they have the same contents.

Assigning an object to a new variable and expecting a copy.

Using objects as keys in a plain object instead of a Map.

FAANG Follow-Up Probes
Probe #1

Write a function that checks whether two plain objects have the same keys and values.

Probe #2

How would you deep-copy an object that contains a Date and nested arrays?

Probe #3

When would you choose a Map over a plain object?

Objects in JavaScript Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for Objects.

Q1.
What are objects in JavaScript?

Collections of key-value pairs (properties). Keys are strings or symbols, values can be any type, and functions stored as values are called methods. Arrays, functions and dates are objects too.

Most often with an object literal { key: value }. Other ways are new Object(), Object.create(proto), a constructor function with new, and a class.

=== and == compare references for objects. Two separately created objects are different objects even if their contents match. Compare their properties, or use a deep-equality helper.

{ ...obj } or Object.assign({}, obj) make a shallow copy: nested objects stay shared. structuredClone(obj) makes a deep copy of most data, but not functions.

Plain-object keys must be strings or symbols, so any other value is converted to a string first, and every plain object converts to '[object Object]'. A Map keeps the object itself as the key.

'key' in obj is true if the property exists on the object or anywhere on its prototype chain. obj.hasOwnProperty('key') and Object.hasOwn(obj, 'key') check only the object's own properties.

Use Object.keys, Object.values or Object.entries with for...of, for example for (const [key, value] of Object.entries(obj)). for...in also works but includes inherited enumerable keys.

More JavaScript interview questions for freshers