Object or null check

I’ve got a json response
can return or nul or an object.
I think about

alert(null instanceof Object);//false
alert({id:10} instanceof Object);//true

Is it the best way ?
In your opinion null is the best value
or could be better like 0 or ‘’ (json)?

Thanks in advance.

Hi Raffles,
thanks for the advice.
I’m a spoilt guy I’m using jquery :slight_smile:
so I made

if(data.user instanceof Object){
     // do somethink  

in data.user can handle this values


usually I make


and than

if( (typeof data.status === 'number')){
      //good value 31
    else {
     //bad value null 
    //typeof null === Object

but in this case may be
I need more data (an object with name surname etc)

Strange way of checking for this. Also:

alert(typeof null); //object
alert(typeof {id:1}); //object

Surely your JSON response is just going to be a string (that’s what JSON is, until you parse it). Still, if it’s been parsed already, why not just this:

if (response === null) {
else if (typeof response === 'Object') {

But I think just checking for 0 is better. Or even just if (!response). It’s hard to say without knowing whether this is after or before parsing JSON and what else is going on.

Make sure you’re using a good JSON parser and not eval, unless you’re very sure about what you’re doing. (see here)