1let person = {
2 firstname: 'John',
3 lastname: 'Doe'
4}
5
6console.log(person.firstname);
7// expected output: "John"
8
9delete person.firstname;
10
11console.log(person.firstname);
12// expected output: undefined
1Removing an element is much easier, as it only requires the element's ID.
21. function removeElement(elementId) {
32. // Removes an element from the document.
43. var element = document. getElementById(elementId);
54. element. parentNode. removeChild(element);
65. }
7
8Example:
9<h1>The remove() Method</h1>
10
11<p>The remove() method removes the element from the DOM.</p>
12
13<p id="demo">Click the button, and this paragraph will be removed from the DOM.</p>
14
15<button onclick="myFunction()">Remove paragraph</button>
16
17<script>
18function myFunction() {
19 var myobj = document.getElementById("demo");
20 myobj.remove();
21}
22</script>
1// delete an element from the dom
2var elem = document.querySelector('#some-element');
3elem.parentNode.removeChild(elem);
4
5
6// keep element in dom
7var elem = document.querySelector('#some-element');
8elem.style.display = 'none';
1var ourDog = {
2 "name": "Camper",
3 "legs": 4,
4 "tails": 1,
5 "friends": ["everything!"],
6 "bark": "bow-wow"
7};
8
9delete ourDog.bark;
1// JavaScript "delete"
2const Employee = {
3 firstname: 'John',
4 lastname: 'Doe'
5};
6
7console.log(Employee.firstname);
8// expected output: "John"
9
10delete Employee.firstname;
11
12console.log(Employee.firstname);
13// expected output: undefined
14
15
16// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete