Note that delete
returns true
when it was successful.
Update 2021: tested on Chrome 88 and Firefox 84:
implicit_global = 1;
delete implicit_global; // true
window.explicit_global = 1;
delete explicit_global; // true
const _object = {property: 1};
delete _object.property; // true
function_set = function() {};
delete function_set; // true
function function_declaration() {};
delete function_declaration; // false
(function () {
var _var = 1;
console.log(delete _var); // false
console.log(_var); // 1
})()
(function () {
let _let = 1;
console.log(delete _let); // false
console.log(_let); // 1
})()
(function () {
const _const = 1;
console.log(delete _const); // false
console.log(_const); // 1
})()
Original answer is no longer relevant. tested in 2015 on Chrome 52:
implicit_global = 1;
window.explicit_global = 1;
function_set = function() {};
function function_dec() { };
var declared_variable = 1;
delete implicit_global; // true, tested on Chrome 52
delete window.explicit_global; // true, tested on Chrome 52
delete function_set; // true, tested on Chrome 52
delete function_dec; // true, tested on Chrome 52
delete declared_variable; // true, tested on Chrome 52