I’m trying to process a complete function in an ajax call. If the value is undefined, I want cast a var as an empty string. Otherwise, I would like to capture the value into a string array.
The problem is I’m entering the if statement, even when logging the value of the variable in question 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 = "";
}
});
}
typeofreturns a string value, so you’ll need to compare with"undefined"as a string. E.g.,Edit – more info:
If you check out the MDN page for typeof, you’ll see this:
This is very different from returning the
Typeitself (which in JavaScript would probably be something like returning a constructor function likeString,Array, etc.). Therefore, when usingtypeof, you’ll always be comparing to strings like"object","string","undefined", etc.