Possible Duplicate:
How do I test for an empty Javascript object from JSON?
var test= {};
var incidentReport = {
"place1": "n/a",
"place2": "n/a",
"place3": "n/a",
}
Above are the two ways my varible is going to look. Ive tryed doing the following code to test if its empty/looks like {}
if(test == "")
and tried
if(test == null)
also tried
if(!test)
Does anyone know where I am going wrong? Just a beginner to JavaScript and JSON. Is what I am doing considered back practice are there better ways to declare this empty?
Thanks for the support
checks if it is an empty string, so this won’t work
checks if it is
nullwhich is “similar” toundefined– this isn’t the casechecks if it is a falsy value, this in not the case either.
You have to check if there exist child-elements (properties):
The very important point is the
.hasOwnProperty()– this checks if it is a real property of the object and not only inherited through the prototype chain.