How to Remove Property from JavaScript Object

Channel: Linux
Abstract: 1delete obj.domainuse the delete operator to delete the specific property from the JavaScript object. delete obj.domain

This tutorial will help you to remove property of a JavaScript object using ‘delete’ operator. On successful deletion, it will return true, else false will be returned. It removes the property value as well as an object property.

Remove JavaScript Object Property

For this example, we have created an object with some default values.

var obj = { "webid": "101", "webname": "TecAdmin", "domain": "www.example.com" };12345var obj = {    "webid": "101",    "webname": "TecAdmin",    "domain": "www.example.com"};

Now, use the delete operator to delete the specific property from the JavaScript object.

delete obj.domain;1delete obj.domain;

Similarly, you can also use the following syntax with the delete operator.

delete obj['domain'];1delete obj['domain'];

Example to Remove JavaScript Object Property

Below is a working example of removing the property from a javascript object. Add below content in an HTML file and access it in a web browser. Now open the console in the browser to view the results.

var obj = { "webid": "101", "webname": "TecAdmin" "regex": "^https://.*" }; console.log('-- Object data before remove --'); console.log(obj); delete obj.regex; console.log('-- Object data after remove --'); console.log(obj);123456789101112var obj = {    "webid": "101",    "webname": "TecAdmin"    "regex": "^https://.*"};console.log('-- Object data before remove --');console.log(obj); delete obj.regex; console.log('-- Object data after remove --');console.log(obj);

Conclusion

In this quick faq, you have learned to remove a properly from a JavaScript object.

Ref From: tecadmin
Channels: jsjavascript

Related articles