JavaScript: delete Operator
Description
The delete operator deletes an object, an object's property, or an element from an array. The operator can also delete variables which are not declared with the var statement.
Version
Implemented in JavaScript 1.2
Syntax
delete objectName delete objectName.property delete objectName[index] delete property // The command acts only within a with statement.
Parameters
objectName:The name of an object.
property: The property is an existing property.
index: An integer representing the array index.
Example:
The following web document demonstrates the use of delete operator.
HTML Code
<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript function operator example with DOM
</title>
<meta name="description" content="This document contains
an example of JavaScript function operator using DOM" />
<style>
h1 {
color:red;
border-bottom: 3px groove silver;
padding-bottom: 8px;
}
</style>
</head>
<h1>JavaScript function operator example</h1>
<script src="javascript-function-operator-example1.js">
</script>
</body>
</html>
JS Code
var fruits = new Array("Orange", "Apple", "Banana", "Chery");
var newParagraph = document.createElement("p");
var newText = document.createTextNode("Fruits List : " + fruits);
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph);
//Delete the array object.
delete fruits;
var newParagraph1 = document.createElement("p");
var newText1 = document.createTextNode("Display the Fruits after delete the array object - Fruits List : "+ fruits;);
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);
See also
Conditional Operator
comma
function
in
instanceof
new
this
typeof
void
Want to Test your JavaScript skill ?
Want to Practice JavaScript exercises ?
- JavaScript basic [ 13 Exercises with Solution ]
- JavaScript functions [ 21 Exercises with Solution ]
- JavaScript conditional statements and loops [ 10 Exercises with Solution ]
- JavaScript array [ 13 Exercise with Solution ]
- JavaScript regular expression [ 6 Exercises with Solution ]
- JavaScript HTML DOM [ 14 Exercises with Solution ]
- JavaScript Drawing [ 5 Exercises with Solution ]
- JavaScript Object [ 4 Exercises with Solution ]
Previous: JavaScript: Comma Operator
Next: JavaScript: function Operator
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
How to insert an item into an array at a specific index (JavaScript)?
What you want is the splice function on the native array object.
arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:
var arr = []; arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; console.log(arr.join()); arr.splice(2, 0, "Lene"); console.log(arr.join());
Ref: https://bit.ly/2BXbp04
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework