javascript - "Cannot read property .. of null" after checking for undefined object -


i have object need check if defined. also, want check if property of object true or false.

so want is

if ((typeof myvar !== 'undefined') && (myvar.ismale === false)) {      // 1  }  else{       // 2  } 

but logic gives me error

uncaught typeerror: cannot read property 'ismale' of null  

what best login handle condition ?

thanks !

you need test further, either exclusion:

if (typeof myvar != 'undefined' && myvar && myvar.ismale === false) { 

or inclusion:

if (typeof myvar == 'object' && mvar && myvar.ismale === false) { 

but there objects return values other "object" typeof tests (e.g. host objects may , function objects do).

or explicit conversion:

if (typeof myvar != 'undefined' && object(myvar).ismale === false) { 

edit

the additional && myvar test catch nan , null pass typeof test.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -