what is the difference between delete an undefined property and a property which declared by const, let using delete operatorin Javascript?

javascript remove from object by key
remove property from object javascript es6
deleting local variable in strict mode
delete multiple properties from object javascript
delete multiple keys from object javascript
remove element from object javascript
javascript delete object
remove property from array of objects javascript

Please look at the code,

foo = 1;
delete foo; // true
Object.getOwnPropertyDescriptor(this,'foo').configurable // true
var bar = 2;
delete bar; // false
Object.getOwnPropertyDescriptor(this,'bar').configurable // false
const fooBar = 3;
Object.getOwnPropertyDescriptor(this,'fooBar').configurable // undefined
delete fooBar; //false
Object.getOwnPropertyDescriptor(this,'noexist').configurable // undefined
delete noexist; // true

Based on MDN the delete operator can work only with properties where their own descriptor configurable is true, so why there is a difference between delete "fooBar" and "noexist" returned value?

Variables declared with const or let do not get assigned to the global object, hence your

const fooBar = 3;

does not show up when you do


Only variables declared with var (or never declared at all, only assigned to, such as with foo) get assigned to the global object.

delete will return:

true for all cases except when the property is an own non-configurable property, in which case, false is returned in non-strict mode.

window.foo, having not been declared with var, let, or const, is a configurable property. window.bar, declared with your var bar, is assigned to window as a non-configurable property.

delete fooBar returns false because fooBar, although not actually a property on window, is a standalone identifier which cannot be deleted - delete will result in false whenever using delete like that would throw an error in strict mode:

'use strict';
const someVar = true;
delete someVar;

delete operator, The JavaScript delete operator removes a property from an object; if no more Any property declared with let or const cannot be deleted from the scope value, use the undefined value instead of the delete operator. In the  The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically. When using the delete operator on arrays take caution as delete will delete the specified item but the array length not get affected.

Appending to the excellent answer from CertainPerformance. When you are assigning values to an undeclared variable like foo = 1, this happens:

Object.defineProperty(window, 'foo', {configurable: true, value: 1});
console.log(delete foo);
console.log(window.foo); //deleted from window

SyntaxError: applying the 'delete' operator to an unqualified name is , Normal variables in JavaScript can't be deleted using the delete operator. In strict mode The delete operator can only delete properties on an object. To free the contents of a variable, you can set it to null : delete · Memory management · TypeError: property "x" is non-configurable and can't be deleted  In this post you'll learn two new ways to create variables in JavaScript (ES6), let and const. Along the way we'll look at the differences between var, let, and const as well as cover topics like function vs block scope, variable hoisting, and immutability.

My understanding is, a variable declared with const, exists, but has the property configurable not set, that's why delete fails (and hence returns false) - the variable still exists.

Apparently deleting an inexistent variable is not an invalid operation, the goal of the operation is to delete the variable, and since it doesn't exist, the operation didn't take place, but the desired effect has been achieved (that's why returns true).

Understanding the Delete Operator in JavaScript, We have an object obj with a property d whose value is 88. So when a property is deleted via the delete operator the value of the property returns undefined. publish, install and update small individual modules across different JavaScript projects. var, let and const declare non-configurable properties. Const behaves in a very similar way to let. It is also block scoped and cannot be used until declared. There is, however, one key difference. Once variable declared using const keyword is assigned a value, you cannot reassign it. You also need to initialize the variable immediately when declaring it.

You cannot delete a constant as stated on MDN.

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

So, when you delete constant fooBar, it returns a false. If something cannot be deleted then you will get false returned by delete statement. So for example, if you tried to delete a var like in var x=20; delete x;//returns false, then you would get a false value since var cannot be deleted within its scope. In the same way const and let variables cannot be deleted within their scopes and they would also return a false.

So, I would think like this : if something cannot be deleted when it is existing then you get a false value returned by the delete statement.

Also, if you try to delete a property that does not exist, then delete statement returns a true as stated on MDN.

If the property which you are trying to delete does not exist, delete will not have any effect and will return true.

So, when you delete noexist property on this object, it returns a true.

JavaScript: The Definitive Guide: Master the World's Most-Used , Undeletable property: returns false, or TypeError in strict mode. delete The value of the await operator is the fulfillment value of the Promise object. legal within functions that have been declared asynchronous with the async keyword. you could also use the void operator in this case: let counter = 0; const increment  Deleting a variable, i.e., delete x; Writing to a read-only property (read-only properties are those defined via Object.defineProperty()). Using variables with names like arguments or eval. Use of the with statement. An answer to this question should include one or more of the above.

Data Structures: Objects and Arrays :: Eloquent JavaScript, It is possible to assign a value to a property expression with the = operator. The difference between setting a property to undefined and actually deleting it is that, in the first JavaScript does this for bindings you declared with let or const  Using delete method is the best way to do that, as per MDN description, the delete operator removes a property from an object. So you can simply write: delete myObject.regex; // OR delete myObject['regex']; The delete operator removes a given property from an object. On successful deletion, it will return true, else false will be returned.

JavaScript and Open Data, Creating an array with the array literal notation The syntax of an “array literal” When typing, do not remove an element, while leaving its comma: the array tu Checking if a variable is an array The operator typeof does not distinguish an array from an object: const t = [a The length property, the index count The lengthis in. Variables declared with let keyword inside the block scope cannot be accessed from outside of the block. Const keyword Variables with the const keyword behave exactly like a variable declared with the let keyword with only one difference, any variable declared with the const keyword cannot be reassigned. Example:

7 Tips to Handle undefined in JavaScript, But JavaScript is different. JavaScript doesn't initialize variables or object properties with null . In this sense, typeof operator returns 'undefined' string for an undefined value: A declared variable that is not yet assigned with a value const and let are block scoped (contrary to older function scoped var )  The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it (see below). Just like const the let does not create properties of the window object when declared globally (in the top-most scope). An explanation of why the name "let" was chosen can be found here. Examples Scoping rules