How to remove property from Object in JavaScript?

November 15, 2017 No comments QA JavaScript Property

The best and right way to do this is to use delete operator.

var sample = {
    "ip": "127.0.0.1",
    "method": "POST",
    "action": "request"
};

// delete ip property 
delete sample.ip;

// or,
delete sample['ip'];

// or,
var prop = "ip";
delete sample[prop];

Delete operator was designed to remove given property from an object. On success it will return true, else false will be returned. Keep in mind that if property doesn't exist, operator will also return true.

{{ message }}

{{ 'Comments are closed.' | trans }}