DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Prevent an object's properties value from updating

const obj = {name: 'Codedrops'};
console.log(obj.name); // Codedrops

/* Set the 'writable' descriptor to false for the 'name' key  */
Object.defineProperty(obj, 'name', {
        writable: false
});

obj.name = 'ABC';
console.log(obj.name); // Codedrops
Enter fullscreen mode Exit fullscreen mode

Thanks for reading πŸ’™

Follow @codedrops.tech for daily posts.

Instagram ● Twitter ● Facebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Top comments (2)

Collapse
 
lowlighter profile image
lowlighter πŸ¦‘

One may look also at Object.seal and Object.freeze, to respectively prevent an object from being extended with new properties or to be modified in any way

Collapse
 
ml318097 profile image
Mehul Lakhanpal

Yes, but there is a small difference between all of them. Have a post coming up..