We help IT Professionals succeed at work.

How to check for both undefined and NULL?

Bruce Gust
Bruce Gust asked
on
High Priority
54 Views
Last Modified: 2020-03-13
Here's my attempt to troubleshoot a problem that I have yet to completely track down...

But I'm getting closer!

  if(typeof json !=='undefined') {
				  console.log("you've got some JSON");
				  console.log(json);
				  console.log(json.chartData);
				  if (json.chartData) {
					  buildChart(json.chartData);
				  }    
			  }
			  else {
				  console.log("no json data");
			  }

Open in new window


This is what I get back:

you've got some JSON
null

So, I've got some JSON, but when I try to see it, I get NULL.

I'm thinking I need a better IF statement, but how?

How do I check for both "undefined" and NULL?

I tried this and it didn't work:  if(typeof json !=='undefined' || json !== NULL) {
Comment
Watch Question

Systems architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
 if(json){
Bruce GustPHP Developer

Author

Commented:
That will do it!