3

In javascript if I declare an object with let, do I need to explicitly delete it?

Example:

downloadHelper(url) {
    let pom = document.createElement('a');
    pom.setAttribute('href', url);
    pom.setAttribute('download', '');
    pom.style.display = 'none';
    document.body.appendChild(pom);
    pom.click();
    document.body.removeChild(pom);
    delete pom;
},
Joe Lane
  • 67
  • 1
  • 6

2 Answers2

2

No you don't JavaScript is garbage collected. Delete only deletes the reference not actually object. Following explains all you needed.

Interesting Posts: Deleting Objects in JavaScript

Community
  • 1
  • 1
Majeed Siddiqui
  • 548
  • 2
  • 9
1

This stackoverflow article may be of use. The short answer is no. Since the variable is still scoped to something, when that something gets deleted then the variable should get garbage collected, often sooner than a var as well.

Ajwhiteway
  • 986
  • 1
  • 9
  • 23