Edit: Based on your comment below, I have a new theory. Since these appear to be some sort of objects and not primitive strings, the length
method is what's causing the trouble:
This simple test behaves exactly the same way as your problem:
var x = {
toString: function() { return 'test'; },
length: function() { return 'something'; }
};
console.log('x is: ' + x);
if (x && x.length && x.length > 0)
console.log('passed');
else
console.log('failed');
It reports to be "test" when you concatenate it to a string, but it fails the test because length > 0
is false because length is a function. I would recommend converting it to a string.
Also, I would point out that some of your tests are unnecessary. salesOrderNumber.length > 0
will be false if and only if salesOrderNumber.length
is falsey. Likewise, salesOrderNumber.length
will only be falsey if and only if salesOrderNumber
is the empty string (for a string), so you can simply:
salesOrderNumber = String(salesOrderNumber);
if (salesOrderNumber)
console.log('ok');
else
console.log('oops');