ES6 - Prevent Object Mutation - -M2I99lSJ5XLWQkZQEvS1

**can you anyone explain what happens when you use console.log after the getting the right answer
**

  **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();
console.log(freezeObj()) // this gives ``` // running tests
// tests completed
// console output
[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']
[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']
3.14
[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']
[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']
3.14 ```
  **Your browser information:**

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

Challenge: ES6 - Prevent Object Mutation

Link to the challenge:

This is just calling the function again, so whatever happened the first time the function is called will happen again. The function is written to intentionally throw an error, which is caught and then the error message is printed to the console. That’s why

[TypeError: Cannot assign to read only property 'PI' of object '#<Object>']

is printed twice in a row. The 3.14 after the second one is the return value of the function, which console.log will print.

The reason you are seeing duplicates of this is just a quirk with how the console works in FCC.

Thanks a lot @bbsmooth, but why is it written to throw the error???

To demonstrate how the freeze method works. Once you freeze MATH_CONSTANTS then you can’t change any of its property values. That’s exactly what the function tries to do and thus that why the error is caught in the catch and is printed to the console.

1 Like

i thought as much, thanks for the time. i appreciate