The JavaScript exception "can't delete non-configurable array element" occurs when it was attempted to shorten the length of an array, but one of the array's elements is non-configurable.
The JavaScript exception "can't delete non-configurable array element" occurs when it was attempted to shorten the length of an array, but one of the array's elements is non-configurable.
TypeError: Cannot delete property '1' of [object Array] (V8-based) TypeError: can't delete non-configurable array element (Firefox) TypeError: Unable to delete property. (Safari)
It was attempted to shorten the length of an array, but one of the array's elements is non-configurable. When shortening an array, the elements beyond the new array length will be deleted, which failed in this situation.
The configurable
attribute controls whether the property can be deleted from the object and whether its attributes (other than writable
) can be changed.
Usually, properties in an object created by an array initializer are configurable. However, for example, when using Object.defineProperty()
, the property isn't configurable by default.
The Object.defineProperty()
creates non-configurable properties by default if you haven't specified them as configurable.
"use strict"; const arr = []; Object.defineProperty(arr, 0, { value: 0 }); Object.defineProperty(arr, 1, { value: "1" }); arr.length = 1; // TypeError: can't delete non-configurable array element
You will need to set the elements as configurable, if you intend to shorten the array.
"use strict"; const arr = []; Object.defineProperty(arr, 0, { value: 0, configurable: true }); Object.defineProperty(arr, 1, { value: "1", configurable: true }); arr.length = 1;
The Object.seal()
function marks all existing elements as non-configurable.
"use strict"; const arr = [1, 2, 3]; Object.seal(arr); arr.length = 1; // TypeError: can't delete non-configurable array element
You either need to remove the Object.seal()
call, or make a copy of it. In case of a copy, shortening the copy of the array does not modify the original array length.
"use strict"; const arr = [1, 2, 3]; Object.seal(arr); // Copy the initial array to shorten the copy const copy = Array.from(arr); copy.length = 1; // arr.length === 3
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Non_configurable_array_element