delete operator should not be used on local variables

  • USELESS_LOCAL_VAR_DELETE
  • Code Quality
  • Low
  • No tags

This rule applies when the delete operator is used on a local variable.

In JavaScript, delete operator does not immediately remove objects from memory. Instead, it just removes references to the objects. When all references to an object are gone, the object will be garbage-collected automatically.

Especially, local variables cannot be explicitly deleted and applying delete operator will have no effect. If the intention is to remove references, values like null should be assigned instead.

Noncompliant Code Example

View with compliant examples side by side
function foo(x) {
    var obj = getObject(x);
    doSomething(obj);
    delete obj; // USELESS_LOCAL_VAR_DELETE alarm because local variable 'obj' cannot be deleted.
    doOther();
}

Compliant Code Example

View with noncompliant examples side by side
function foo(x) {
    var obj = getObject(x);
    doSomething(obj);
    obj = null;
    doOther();
}

Version

This rule was introduced in DeepScan 1.31.0.

See

Was this documentation helpful?