Reputation: 4306
I want to check if my json data is null/undefined. I have the following code but it seems to skip the checking and go straight to the else
.
if (events === null) {
container.append(nano(nogig));
} else {
events[0].start.good_date = good_date(events[0].start.date);
container.append(nano(template, events[0]));
}
Line 385 on this JSfiddle
Upvotes: 0
Views: 885
Reputation: 150070
If you use the ==
operator rather than the ===
operator then testing against null
will also match on undefined
. That is undefined == null
will return true
, while undefined === null
will return false
. So try this:
if (events == null) {
Demo: http://jsfiddle.net/5WQxq/1/
Alternatively you can explicitly test for both values:
if (events === null || events === undefined) {
Or you can just test for a falsy value:
if (!events) {
(...which would also match on false
, 0
, empty string, and NaN
, but those values aren't likely to apply and in any case if they did occur you'd still want the if
case to match.)
Upvotes: 1
Reputation: 1910
if (!events) {
container.append(nano(nogig));
} else {
events[0].start.good_date = good_date(events[0].start.date);
container.append(nano(template, events[0]));
}
This checks for null and undefined.
Upvotes: 3