JavaScript TypeError – Can’t delete non-configurable array element

This JavaScript exception can’t delete non-configurable array element that occurs if there is an attempt to short array-length, and any one of the array’s elements is non-configurable.

Message:

TypeError: can't delete non-configurable array element (Firefox)
TypeError: Cannot delete property '2' of [object Array] (Chrome)

Error Type:

TypeError

Cause of error: When one of the array’s elements is non-configurable and code tries to shorten the length of the array.

Example 1: In this example, the array properties are non-configurable and an attempt was made to delete the property by shortening the array length. 



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var array = [];
    Object.defineProperty(array, 1, {value: 4}); 
    Object.defineProperty(array, 2, {value: "4"});
    array.length = 1; // Error here
</script>

chevron_right


Output:

TypeError: can't delete non-configurable array element

Example 2: In this example, the array properties are non-configurable and an attempt was made to delete the property by shortening the array length. 

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    var array = ['a', 'b', 'c'];
    Object.seal(array);
    array.length = 1; // Error here
</script>

chevron_right


Output:

TypeError: can't delete non-configurable array element

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.