The syntax for deleting property from objects
var student1 = new object(); student1.roll = 1; student1.name = "john"; document.write(student1.roll, "<br/>"); // 1 document.write(student1.name, "<br/>"); // john delete.student1.roll; document.write(student1.roll, "<br/>"); // undefined
- See the above code, we have created the object “student1”.
- Then we created the “roll” property for object “student1” and assigned the value “1”. And we created the “name” property for object “student1” and assigned the value “john”.
- If I want to display only roll of the student1, so we print “student1.roll”.
- Then I want to display only the name of the student1, so we print “student1.name”.
- To delete the property “roll” to the object “student1”, we should use the “delete” keyword and then put the object name and propertyname.
- We printed “student1.roll” after deleting the property, so it returns undefined.