DEV Community

loading...

Prevent an object's properties value from updating

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read
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.

InstagramTwitterFacebook

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

codedrops.tech

Discussion (2)

pic
Editor guide
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
318097 profile image
Mehul Lakhanpal Author

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