Skip to content

Files

Latest commit

 

History

History
36 lines (26 loc) · 629 Bytes

no-delete-var.md

File metadata and controls

36 lines (26 loc) · 629 Bytes

Pattern: Use of delete on variable

Issue: -

Description

The delete operator is designed to remove properties from objects. Using it on variables can lead to unexpected behavior and is not the correct way to unset variables. Use assignment to undefined or block scoping instead.

Examples

Example of incorrect code:

var x;
delete x;

let y = 42;
delete y;

function test() {
  var z = 43;
  delete z;
}

Example of correct code:

let obj = { x: 1, y: 2 };
delete obj.x;

var x = 42;
x = undefined;  // To unset a variable

{
  let y = 43;
}  // Variable goes out of scope