How can I check a value for null in jquery without getting an error?
I've got this code:
- if (returneddata.daterangeparams.TimeUnitsFrom != null) {
...which throws this error (as seen in the Chrome Dev Tools console) when the value is, indeed, null:
Index:1031 Uncaught TypeError: Cannot read property 'TimeUnitsFrom' of null
So how can I check for null in a way that I can avoid the error?
Based on a suggestion
here, I even tried this:
- if (returneddata.daterangeparams.TimeUnitsFrom != null && variable !== undefined) {
...but I still get the same whinging from the guts of the browser.
Code in greater context:
- function populatedaterangeprams(rptval, returneddata) {
var fromval = '';
var toval = '';
if (returneddata.daterangeparams.TimeUnitsFrom != null &&
returneddata.daterangeparams.TimeUnitsFrom !== undefined) {
fromval = returneddata.daterangeparams.TimeUnitsFrom;
}
if (returneddata.daterangeparams.TimeUnitsTo != null &&
returneddata.daterangeparams.TimeUnitsTo !== undefined) {
toval = returneddata.daterangeparams.TimeUnitsTo;
}
if (rptval === 1) {
// Produce Usage
$("#produsagefrom").val(fromval);
$("#produsageto").val(toval);
} else if (rptval === 2) {
. . .
So how can I safely check for null?