Skip to content

Commit 89d4665

Browse files
committed
exec: what-is-objectfreeze-for
1 parent 02748b9 commit 89d4665

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

questions/what-is-objectfreeze-for/en-US.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ title: What is `Object.freeze()` for?
66

77
`Object.freeze()` is used to make an object immutable. Once an object is frozen, you cannot add, remove, or modify its properties. This is useful for creating constants or ensuring that an object remains unchanged throughout the program.
88

9-
```js
9+
```js live
1010
const obj = { name: 'John' };
1111
Object.freeze(obj);
1212
obj.name = 'Doe'; // This will not change the name property
13+
14+
console.log(obj); // { name: 'John' }
1315
```
1416

1517
---
@@ -22,7 +24,7 @@ obj.name = 'Doe'; // This will not change the name property
2224

2325
To use `Object.freeze()`, simply pass the object you want to freeze as an argument to the method. Here is a basic example:
2426

25-
```js
27+
```js live
2628
const obj = {
2729
name: 'John',
2830
age: 30,
@@ -54,7 +56,7 @@ console.log(obj); // Output: { name: "John", age: 30 }
5456
- `Object.freeze()` only makes the object itself immutable. If the object contains nested objects, those nested objects are not frozen and can still be modified.
5557
- To deeply freeze an object, you would need to recursively apply `Object.freeze()` to all nested objects.
5658

57-
```js
59+
```js live
5860
const deepFreeze = (obj) => {
5961
Object.keys(obj).forEach((key) => {
6062
if (typeof obj[key] === 'object' && obj[key] !== null) {

0 commit comments

Comments
 (0)