Skip to content

OutputNot attempted

Property flags in strict mode

const user = {};
Object.defineProperty(user, 'id', { value: 101, writable: false, enumerable: true, configurable: true });
Object.defineProperty(user, 'secret', { value: '123', enumerable: false });
console.log(Object.keys(user), user.secret);
try {
user.id = 999;
} catch (e) {
console.log(e.name);
}
console.log(user.id);

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Non-enumerable properties are hidden from Object.keys / for...in / JSON.stringify but still readable directly. Writing to a non-writable property fails silently in sloppy mode and THROWS TypeError in strict mode (modules and classes are always strict).