1

I want to ask which is the right way to check if an ajax response object has a property . I googled and what I found is that there are different ways to approach this.

For example:

    if(ajaxResponse.hasOwnProperty('someProperty')){
       //do blah blah
    }

but there are other ways for sure, for example :

    if(typeof ajaxResponse.someProperty !== 'undefined')){
       //do blah blah
    }

So I am guessing there are some pros and cons can you please share these with me.

Thanks

  • The hasOwnProperty is more acceptable I think – chenop Aug 18 '16 at 06:18
  • @chenop is right, but you could also do something like: `var check = ajaxResponse.someProperty || false;` – Derek Pollard Aug 18 '16 at 06:19
  • @ chenop Yes I thought so too but nowadays I am working in an existing project and they use the second way, but they are clever people ( of course ) so there must be a reason for that, that I cannot see at the moment. – Paschalidis Christos Aug 18 '16 at 06:20
  • 1
    It depends on what property you're checking and for what purpose. There are different ways to do it because some are more appropriate in some cases and others are more appropriate in other cases. Without knowing the context it's impossible to say. – JJJ Aug 18 '16 at 06:21
  • In a general sense the `typeof` check does *not* check if an object has a property, because it may be that the object does have that property but the property's value is `undefined`. – nnnnnn Aug 18 '16 at 06:29

3 Answers3

1

Let's say your an object is something like the one showed below...

var person = {
    name: "Nicholas"
};

there are plenty of methods using which you can check for this....

Method 1

person.hasOwnProperty("name")

Method 2

if ("name" in person){
    //property exists
}

Method 3 (NOT RECOMMENDED)

//doesn't accurately test for existence
if (person.name){
    //yay! property exists!
}

If you just want to check for the existence of properties, and not necessarily what their value might be, then you have two safe options: hasOwnProperty() and the in operator. The hasOwnProperty() property method should be used if you want to detect own properties only. If you want to test property existence and don’t care if it’s an own property or an object property, then the in operator is the one to use.

Source

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • if `person.name` is falsey, you test fails on `if (person.name) { ... }`. – Nina Scholz Aug 18 '16 at 06:25
  • Umm... @NinaScholz you are right. But end of the day, its his contract, the person writing and accessing the object knows the type of the property, if its a bool then "Method 3" should be avoided. And I have already added a similar note for the same. – Yasser Shaikh Aug 18 '16 at 06:29
  • If you're testing specifically for a *name* then method 3 is probably OK, because even if the property exists as an empty string that *may* be logically equivalent to not having a name at all. But with the OP's more generic `someProperty` it's not suitable. – nnnnnn Aug 18 '16 at 06:33
  • @NinaScholz Agreed. Method 3 is not recommended. – Yasser Shaikh Aug 18 '16 at 06:37
0
 $.ajax({
            type: "POST",
            url: "frmSample.aspx/LoadSample",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {     
               var jsonData = $.parseJSON(data.d);    
            },
            error: function OnError(xhr) {           
            }
        });

Suppose if any error occurs in ajax call the response goes directly to the error, it will not go to success. If empty data is passed as result, then data.d would be ''. So, it is neccessary to check whether data.d is empty or not before parsing. If the resultant is Json, parse the json as follows

var jsonData = $.parseJSON(data.d);

If jsonData.length == 0, no property is there for the result. There are many ways of checking the property in json response. This is one way of doing it in simple. Directly check the jsonData.PropertyName != null or not.

Vaishali
  • 117
  • 1
  • 2
  • 9
-2

the easiest way imo is:

if (ajaxResponse.someProperty){
//do stuff
}

Except the property is a boolean. Then this would not necessarily work as wanted :)

Herr Derb
  • 4,977
  • 5
  • 34
  • 62
  • 1
    Doesn't work if the property is a number that might be `0`. Or a string that might be an empty string. Or might be null. Or might exist with the value `undefined`. – nnnnnn Aug 18 '16 at 06:30