Only properties should be deleted

History

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three tools ever since.

  • In JSLint the warning given is "Only properties should be deleted"

  • In JSHint and ESLint the warning has always been "Variables should not be deleted"

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "Only properties should be deleted" error, and the alternative "Variables should not be deleted" error, is thrown when JSLint, JSHint or ESLint encounters the delete operator followed by a single identifier. In the following example we declare a variable x and then attempt to delete it:

var x = 10;
delete x;

Why do I get this error?

This error is raised to highlight code that probably doesn't work as you expect it to. It can also indicate a fatal syntax error. The delete operator will only delete properties of objects. It cannot "delete" variables or anything else. Here's a valid use of the delete operator. Notice how this time there are no JSLint errors:

var x = {
    prop: 10
};
delete x.prop;

The ECMAScript 5 specification details the behaviour of the delete operator (ES5 §11.4.1). When the operand is a reference to an object property this is what happens:

If IsPropertyReference(ref) is true, then
    Return the result of calling the [[Delete]] internal method on ToObject(GetBase(ref)) providing GetReferencedName(ref) and IsStrictReference(ref) as the arguments.

But when the operand is a reference to an Environment Record binding (something that is not an object property), the runtime will attempt to delete it (and fail) unless the code is running in strict mode. In that case a syntax error is thrown:

Else, ref is a Reference to an Environment Record binding, so
    If IsStrictReference(ref) is true, throw a SyntaxError exception.
    ...[attempt to delete]...

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W051. This means you can tell JSHint to not issue this warning with the /*jshint -W051 */ directive.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.