2018-10-12 15:37:13 -04:00
---
title: Prevent Object Mutation
---
 Remember to use < a > **`Read-Search-Ask` **</ a > if you get stuck. Try to pair program  and write your own code 
### Problem Explanation:
2019-03-10 19:07:48 +01:00
_You need to freeze the `MATH_CONSTANTS` object so that no one is able to alter the value of `PI` , add, or delete properties ._
2018-10-12 15:37:13 -04:00
##  Hint: 1
2019-03-10 19:07:48 +01:00
* _Use `Object.freeze()` to prevent mathematical constants from changing._
2018-10-12 15:37:13 -04:00
> _try to solve the problem now_
## Spoiler Alert!

2019-03-10 19:07:48 +01:00
**Solution Ahead!**
2018-10-12 15:37:13 -04:00
2019-03-10 19:07:48 +01:00
##  Basic code solution:
2018-10-12 15:37:13 -04:00
```javascript
function freezeObj() {
"use strict";
const MATH_CONSTANTS = {
PI: 3.14
};
Object.freeze(MATH_CONSTANTS);
try {
MATH_CONSTANTS.PI = 99;
} catch( ex ) {
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
```
# Code Explanation:
By using Object.freeze() on `MATH_CONSTANTS` we can avoid manipulating it.
2019-03-10 19:07:48 +01:00
### Resources
- ["Object.freeze()" - *MDN Javascript reference* ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze )