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.
let cannot be deletedvar, 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
letorconstcannot be deleted from the scope within which they were defined.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With