diff --git a/src/data/question-groups/javascript/content/immutable-object.md b/src/data/question-groups/javascript/content/immutable-object.md new file mode 100644 index 000000000..81ddb9e2c --- /dev/null +++ b/src/data/question-groups/javascript/content/immutable-object.md @@ -0,0 +1,12 @@ +To make an object immutable, you can use `Object.freeze()` method. It prevents the modification of existing property values and prevents the addition of new properties. + +```js +const roadmap = { + name: 'JavaScript', +}; + +Object.freeze(roadmap); + +roadmap.name = 'JavaScript Roadmap'; // throws an error in strict mode +console.log(roadmap.name); // JavaScript +``` diff --git a/src/data/question-groups/javascript/javascript.md b/src/data/question-groups/javascript/javascript.md index 1b1766fc4..3aeee808f 100644 --- a/src/data/question-groups/javascript/javascript.md +++ b/src/data/question-groups/javascript/javascript.md @@ -206,4 +206,9 @@ questions: topics: - 'Core' - 'Advanced' + - question: How to make an Object immutable in JavaScript? + answer: immutable-object.md + topics: + - 'Core' + - 'Advanced' ---