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

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 been occurred.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var GFG_Obj = Object.create({});
Object.defineProperty(GFG_Obj, "prop1", {value: "val1"}); 
Object.defineProperty(GFG_Obj, "prop1", {value: "val2"});
</script>

chevron_right


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.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
var Obj = Object.create({"prop1": "val1"});
Object.defineProperty(Obj, "prop2", {value: "val21"}); 
Object.defineProperty(Obj, "prop2", {value: "val22"});
</script>

chevron_right


Output:

TypeError: Cannot modify non-writable property 'prop2'

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.