Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript TypeError – Can’t delete non-configurable array element

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. 

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments