Implement the removeProperty function to take an object and a property name, delete the property - Javascript Object

Javascript examples for Object:Object Property

Description

Implement the removeProperty function to take an object and a property name, delete the property

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <h1>JavaScript Functions</h1> 
      <p onclick="removeProperty(obj,'name')">This example calls :</p> 
      <script>
function removeProperty(obj,prop){
if(obj.hasOwnProperty(prop)) {//from w ww  . java 2  s .  c  om
    console.log(obj);
    var b = delete obj.prop;
    console.log(b);
    return true;
  }
  else {
      return false;
    }
}
var obj= {
  name:"John"
};

removeProperty(obj,'name');

      </script>  
   </body>
</html>

Related Tutorials