JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – add new key in object after freeze is possible

JavaScript Quiz – add new key in object after freeze is possible

JavaScript Object freeze –

JavaScript object freeze is used to freeze object which disallow any change in object. object freeze is difference from declaring object with const keyword.

Because const object can not be reassigned but it’s key value still can be changed.

It’s syntax look like –

Object.freeze(obj)

For more information about javascript Object freeze click here.

JavaScript Question

In this javascript quiz code, let’s find what happen when add new key in object after object is freeze as it not allow change in key value.

First, we declare a variable detail using the let keyword and assign a javascript object to it. Which contains only the name key.

let detail = { name : “JS Startup” };

In the second step, we use a Object.freeze() method to detail variable and then change it’s name value to “JavaScript“.

Object.freeze(detail);

detail[‘name’] = “JavaScript”;

But when we checked the value of detail name key it still the same “JS Startup”. That means, object freeze is in action.

console.log(detail.name); // output => JS Startup

Adding New Key –

In the next step, instead of changing the value of a key we added a new key active in object and assign value “true” of it.

detail[‘active’] = “true”;

Finally, let’s check the value of detail active key using console log.

console.log(detail.active); // output => ?

Any idea what should be the output of the above console log.

Answer – undefined

Click To Run Code

Explanation

Now, you know the answer is undefined. But now, we not change the current value of the name key. It’s just adding new value. Let me explain

Object freeze is an important method to freeze the object. So it not only just restrict the change in the value of the key. But also not allow adding a new key in the object.

Even if you check the keys in the object after adding the active key but you will not find it.

At last, That’s why we get an answer “undefined“.

Let me know in the comment section, how to solve this also.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – can setTimeout in the for loop print values
  2. JS Quiz – can reduceRight method really sum an array elements
  3. JS Quiz – includes method can really remove duplicates with forEach
  4. JavaScript Quiz – can delete operator delete a global variable

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link