0

Using Chrome I passed in console this name of global variable:

multiConfig

And get result:

multi: {type: "1", containerId: "mp", go: {…}}
__proto__: Object

I try to delete this variable by click:

if (window['multiConfig']) {
    delete window['multiConfig'];
}

And I get this error:

ERROR TypeError: Cannot delete property 'multiConfig' of [object Window]

Why?

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103

2 Answers2

1

You cannot delete a window variable, but you can unset it:

window['multiConfig'] = undefined;

Reason:

The window object is not configurable.
You can refer to this - How to unset a JavaScript variable?

Aniket Kolekar
  • 383
  • 6
  • 19
  • Could I delete element from global obnjet by key or only undefine too? `var multiConfig = {key: {props}}` I need delete element by key –  Jun 10 '21 at 09:12
  • 3
    Yes, you can mark any key as `undefined` and it will unset it. –  Jun 10 '21 at 09:17
  • Okay, but then I get globval object with a lot of keys and their values are undefined. Could I delete and keys? –  Jun 10 '21 at 09:20
  • Using a lot of global variables will make your `window` heavy - which is not recommended. Try not to overload the `window` if possible and use some state management libraries (like Redux) for maintaining global states. – Aniket Kolekar Jun 10 '21 at 09:23
  • No, I mean I have only one global object , custom with some keys, could I delete all keys in this object? –  Jun 10 '21 at 09:30
  • This will give you a clearer picture - Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete – Aniket Kolekar Jun 10 '21 at 09:34
0

Variables declared with var are added as properties on the global window object and cannot be deleted with the delete operator.

From MDN - var:

In the global context, a variable declared using var is added as a non-configurable property of the global object. This means its property descriptor cannot be changed and it cannot be deleted using delete.

Reason for this is also explained:

The property created on the global object for global variables, is set to be non-configurable because the identifier is to be treated as a variable, rather than a straightforward property of the global object. JavaScript has automatic memory management, and it would make no sense to be able to use the delete operator on a global variable.

Also note that trying to delete a property set on of global window object for variables declared with var fails silently in non-strict mode and throws a TypeError in strict-mode.

Also note that you can delete a property from the window object if is set explicitly.

var a = 1;      // can't be deleted
window.b = 2;   // can be deleted

delete window.a;
delete window.b;

console.log(window.a);
console.log(window.b);
Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • Thank you, what if I use this window.config = {key: 1, key2: 2}; and want to delete key2 to get this: {key: 1}? –  Jun 10 '21 at 09:32
  • You can do: `var config = {key: 1, key2: 2}; delete config.key2;` or `delete window.config.key2;`. – Yousaf Jun 10 '21 at 09:36