2018-09-30 23:01:58 +01:00
---
id: 598f48a36c8c40764b4e52b3
title: Prevent Object Mutation
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301207
2021-01-13 03:31:00 +01:00
dashedName: prevent-object-mutation
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
As seen in the previous challenge, `const` declaration alone doesn't really protect your data from mutation. To ensure your data doesn't change, JavaScript provides a function `Object.freeze` to prevent data mutation.
2018-09-30 23:01:58 +01:00
Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.
2019-05-17 06:20:30 -07:00
```js
let obj = {
name:"FreeCodeCamp",
review:"Awesome"
};
Object.freeze(obj);
obj.review = "bad"; // will be ignored. Mutation not allowed
obj.newProp = "Test"; // will be ignored. Mutation not allowed
console.log(obj);
// { name: "FreeCodeCamp", review:"Awesome"}
```
2020-11-27 19:02:05 +01:00
# --instructions--
In this challenge you are going to use `Object.freeze` to prevent mathematical constants from changing. 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-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should not replace `const` keyword.
```js
(getUserInput) => assert(getUserInput('index').match(/const/g));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`MATH_CONSTANTS` should be a constant variable (by using `const` ).
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
assert(getUserInput('index').match(/const\s+MATH_CONSTANTS/g));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should not change original `MATH_CONSTANTS` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
assert(
getUserInput('index').match(
/const\s+MATH_CONSTANTS\s+=\s+{\s+PI:\s+3.14\s+};/g
)
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`PI` should equal `3.14` .
```js
assert(PI === 3.14);
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function freezeObj() {
const MATH_CONSTANTS = {
PI: 3.14
};
2020-03-04 13:08:54 -06:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-03-04 13:08:54 -06:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
try {
MATH_CONSTANTS.PI = 99;
2019-03-29 09:36:58 -07:00
} catch(ex) {
2018-09-30 23:01:58 +01:00
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2018-10-16 05:34:52 +05:30
function freezeObj() {
const MATH_CONSTANTS = {
PI: 3.14
};
Object.freeze(MATH_CONSTANTS);
try {
MATH_CONSTANTS.PI = 99;
2019-03-29 09:36:58 -07:00
} catch(ex) {
2018-10-16 05:34:52 +05:30
console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
2018-09-30 23:01:58 +01:00
```