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 http://stackoverflow.com/questions/12271750/how-to-check-if-a-variable-is-both-null-and-or-undefined-in-javascript, 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) {
$("#produsagefrom").val(fromval);
$("#produsageto").val(toval);
} else if (rptval === 2) {
. . .
So how can I safely check for null?
What I have tried:
Based on a suggestion here http://stackoverflow.com/questions/12271750/how-to-check-if-a-variable-is-both-null-and-or-undefined-in-javascript, I even tried this:
if (returneddata.daterangeparams.TimeUnitsFrom != null && variable !== undefined) {
...but I still get the same whinging from the guts of the browser.