ES6 - Prevent Object Mutation

Tell us what’s happening:

Describe your issue in detail here.
Good evening, i have been trying to use the Object.freeze(MATH_CONSTANTS);
i keep having : “[TypeError: Cannot assign to read only property ‘PI’ of object ‘#’]”

Your code so far

function freezeObj() {
  const MATH_CONSTANTS = {
    PI: 3.14
  };
  // Only change code below this line
 Object.freeze(MATH_CONSTANTS);

  // Only change code above this line
  try {
    MATH_CONSTANTS.PI = 99;
  } catch(ex) {
    console.log(ex);
  }
  return MATH_CONSTANTS.PI;
}
const PI = freezeObj();

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

ES6 - Prevent Object Mutation

That means the code is working correctly. Your tests pass

2 Likes

To expand on Jeremy’s comment, the pupose of this challenge is to show that sometimes it is important that you do not change (“mutate”) a variable and that JavaScript provides tools for ensuring that.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.