CodePark/ questions / Equality & copying
START PRACTICINGPRACTICE
Frontend question · JavaScript › Equality & copying

In a non-module script (sloppy mode), what does this log?

const cfg = Object.freeze({ port: 80, db: { host: "a" } });
cfg.port = 8080;
cfg.db.host = "b";
console.log(cfg.port, cfg.db.host);
80 b

The answer is 80 b. Object.freeze is shallow. It makes the top-level properties read-only, so the port write fails silently. But the nested db object is not frozen, so its host can change.

The tempting option "It throws a TypeError" is wrong here because this is sloppy (non-strict) mode. In strict mode, including ES modules, the
port assignment would throw a TypeError.