Open In App

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

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Javascript




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


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. 

Javascript




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


Output:

TypeError: can't delete non-configurable array element

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

Similar Reads