Open In App

JavaScript TypeError – Can’t redefine non-configurable property “x”

Last Updated : 02 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This JavaScript exception can’t redefine non-configurable property occurs if user tries to redefine a property, but that property is non-configurable.

Message:

TypeError: Cannot modify non-writable property {x} (Edge)
TypeError: can't redefine non-configurable property "x" (Firefox)
TypeError: Cannot redefine property: "x" (Chrome)

Error Type:

TypeError

Cause of the Error: If there is an attempt to redefine a property, but that property is non-configurable.

Example 1: In this example, the ‘prop1’ is being attempted to change the value, so the error has occurred.

Javascript




let GFG_Obj = Object.create({});
Object.defineProperty(GFG_Obj, "prop1", { value: "val1" });
Object.defineProperty(GFG_Obj, "prop1", { value: "val2" });


Output:

TypeError: Cannot modify non-writable property 'prop1'

Example 2: In this example, the ‘prop2’ is being attempted to change the value, So the error has occurred.

Javascript




let Obj = Object.create({ "prop1": "val1" });
Object.defineProperty(Obj, "prop2", { value: "val21" });
Object.defineProperty(Obj, "prop2", { value: "val22" });


Output:

TypeError: Cannot modify non-writable property 'prop2'

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads