Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete JavaScript function (declared with let) from chrome developer console

I defined the following function in a chrome developer console-

 let splitNumber=function(num){ 
                  let arr=[];
                  arr=num.toString().split(""); 
                 return arr};

Then I tried to delete this function from the console scope using this command -

 delete window.splitNumber
  > true 

Now if I try to use the same variable 'splitNumber' again in the console, I get this error -

Uncaught SyntaxError: Identifier 'splitNumber' has already been declared

Can someone please help me understand what I am doing wrong here? Please let me know if the question is not clear.

like image 438
Shibasis Sengupta Avatar asked Dec 05 '25 07:12

Shibasis Sengupta


1 Answers

A property declared with let cannot be deleted

var, let, and const create non-configurable properties which cannot be deleted with the delete operator. Since you declared the function with let, it cannot be deleted.

Any property declared with let or const cannot be deleted from the scope within which they were defined.


Why does delete return true?

Generally, when a property is non-configurable and cannot be removed, delete will return false. So you may be wondering why your delete statement returns true.

Well, delete returns true if the property does not exist after deletion. This means it will return true either if the delete was successful or if the property never existed in the first place. Unlike var, let does not create a property on the global object. So delete window.splitNumber returns true because splitNumber does not exist as a property of the global window object.

If you try delete splitNumber, it will return false indicating that splitNumber exists in the global scope and cannot be deleted.

like image 65
Chava Geldzahler Avatar answered Dec 07 '25 21:12

Chava Geldzahler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!