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
Post a Comment