โdelete
๐ง under construction -> configurable
Last updated
๐ง under construction -> configurable
Last updated
JS โฉ statement โฉ expression โฉ operator โฉ unary โฉ delete
deletes configurable property / element.
delete <expr>
// โญ๏ธ return true if `expr` is not a "property access expression"
๐ table of operators
delete
can't delete normal variables. they are considered unqualifiedโ
only deletes object's own/configurable properties, not inherited ones.
expects its operand to be an lvalue, if it is not an lvalue, the operator takes no action and returns true.
deleting an array element leaves a โholeโ in the array and does not change the arrayโs lengthโ (sparse array)
๐ฐ global variable/function as global object property
these properties cannot be deleted with deleteโ
delete <expr>
returns true if <expr>
is not a property access expression.
accidental global variable in sloppy modeโ๏ธ - can be deleted by delete.
array elements can be deleted with the delete operator.
replit๏ผdelete in sloppy/strict mode
// 'use strict'; // โญ toggle sloppy/strict mode
const { log } = console;
// declarations
let x = 3.14; // variable
function f1() { log('f1'); } // function declaration
let f2 = function() { log('f2'); } // function expression
let fruits = ["Banana", "Orange"]; // array
let person = { name: 'John', age: 18 }; // object
// log
[
// mode: // sloppy strict
// -----------------------------------------------------------
delete x, // โ false โ SyntaxError
x, // 3.14
delete f1, // โ false โ SyntaxError
f1, // [Function: f1]
delete f2, // โ false โ SyntaxError
f2, // [Function: f2]
delete fruits[1], // โ
true โ
true
fruits, // ['Banana', <empty>] ['Banana', <empty>]
delete person.age, // โ
true โ
true
person, // { name: 'John' } { name: 'John' }
].forEach(x => console.log(x));
// โ SyntaxError: Delete of an unqualified identifier in strict mode.