javascript object delete code example

Example 1: javascript delete key from object

let person = {
  firstname: 'John',
  lastname: 'Doe'
}

console.log(person.firstname);
// expected output: "John"

delete person.firstname;

console.log(person.firstname);
// expected output: undefined

Example 2: delete kvp object

// remove key-value pair from object
let person = {
  name: 'Bob',
  age: 34
};

delete person['age'];
console.log(person); // { name: 'Bob' }

Example 3: javascript remove property from object

var person = {"first_name": "Billy","last_name": "Johnson"};
delete person.last_name; //delete last_name property

Example 4: how to remove a property from an object in javascript

var person = {
        name: "Harry",
        age: 16,
        gender: "Male"
    };
    
    // Deleting a property completely
    delete person.age;
    alert(person.age); // Outputs: undefined
    console.log(person); // Prints: {name: "Harry", gender: "Male"}

Example 5: javascript remove object key

var obj = {a: 5, b: 3};
delete obj["a"];
console.log(obj); // {b: 3}

Example 6: javascript remove element from object

delete object.element