We can't delete a local variable
that has been declared with var
/let
/const
.
We can only delete properties of objects. This also includes global variables which are implicit properties of the window object. As per the MDN --
The JavaScript delete operator removes a property from an object
If we change the code to the following then delete will work.
window.person = "Yomesh";
var deletePerson = () => {
delete window.person;
return window.person;
};
console.log(deletePerson()); // undefined
To read more -- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
For useful and amazing frontend and programming tutorials: https://bit.ly/devtools-yt
P.S. - Do not forget to share this question with others! :D