Home > Net >  Deleting a non-string in js object
Deleting a non-string in js object

Time:10-09

Let's say I have the following object:

const a = {a: 1, 0: 2};
delete a.a; // this works
delete a.0; // this errors (I suppose expecting a['0'] and not a[0])

Does the method deletion form of obj.key only allow deletion of a string key? And if it's a non-string type, is the only way of doing deletion via the key lookup method of obj[key] ?

CodePudding user response:

According to MDN:

In the object.property syntax, the property must be a valid JavaScript identifier.

... where ...

... identifiers are case-sensitive and can contain Unicode letters, $, _, and digits (0-9), but may not start with a digit.

So, answering your question, property with name 0 can be only accessed with key notation.

  • Related