Problems with undefined! == undefined

I am trying to handle the full function in an ajax call. If the value is undefined, I want var var to be an empty string. Otherwise, I would like to write the value to an array of strings.

The problem is that I enter the if statement, even when the value of the variable in question is logged, it returns as undefined. What am I missing here?

completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        if(typeof $(this).attr("ows_Products") !== undefined) {
          console.log($(this).attr("ows_Products"));
          arr = $(this).attr("ows_Products").split(',');
        }
        else {
          arr = "";
        }
      });
    }
+5
source share
2 answers

typeofreturns a string value, so you need to compare with "undefined"how string. For instance.

if(typeof $(this).attr("ows_Products") !== "undefined") { ... }

Edit - details:

If you select the MDN page for typeof , you will see the following:

The typeof operator returns a string indicating the type of inexperienced operand.

Type ( JavaScript, , - - String, Array ..). typeof "object", "string", "undefined" ..

+16
if($(this).attr("own_Products")){
       arr = $(this).attr("ows_Products").split(',');
}else{
       arr=""
}
0

All Articles