Daily JavaScript Quiz - 2024-04-10

  • Home /
  • Daily JavaScript Quiz - 2024-04-10

Daily Quiz - 2024-04-10

const a = { a: 1 };
const b = Object.seal(a);
b.a = 2;
console.log(a.a);

1

undefined

TypeError

2

The answer is Option 4

  1. const a = { a: 1 };: This line creates an object a with a property a set to the value 1.

  2. const b = Object.seal(a);: Here, the Object.seal() method is used to seal the object a. Sealing an object in JavaScript means that you can’t add or remove properties from it, though you can still modify the existing properties. The sealed object is returned and assigned to variable b.

  3. b.a = 2;: This line attempts to modify the property a of the sealed object b to 2.

  4. console.log(a.a);: Finally, this logs the value of property a of the original object a to the console.

Given that a was sealed by Object.seal(), modifying b.a does affect the property a of the original object a.

So, when you run this code, it will output 2, because the modification to b.a is reflected in a.a. Since a was sealed, the modification is allowed.

Javascript Quizzes