I am trying to test to see whether a Javascript variable is undefined.
You will see that I am not expecting the value of predQuery[preId] to be 'undefined' if I don't first get an alert saying "its unbelievable". But I often do, so I am guessing that my statement
predQuery[preId]=='undefined')
is not matching the undefined elements properly.
if((predQuery.length < preId) || (predQuery[preId]=="") || (predQuery[preId]=='undefined')){ alert("its unbelievable"); alert(predQuery[preId]); queryPreds[variables] = preId; queryObjs[variables] = objId; predQuery[preId] = variables; } else { alert(predQuery[preId]); var predIndex = predQuery[preId]; queryPreds[predIndex] = preId; queryObjs[predIndex] = objId; }
I can add more code if needed.
To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.
Use the Array. includes() method to check if an array contains undefined values, e.g. arr. includes(undefined) . The includes method will return true if the array contains at least one undefined value and false otherwise.
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.
JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.
array[index] == 'undefined'
compares the value of the array index to the string "undefined".
You're probably looking for typeof array[index] == 'undefined'
, which compares the type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With