admin管理员组文章数量:1356591
I have an object that I need to check if it is defined. Also, I also want to check if a property of that object is true or false.
So what I want is
if ((typeof myVar !== 'undefined') && (myVar.isMale === false)) {
// Do something 1
}
else{
// Do something 2
}
But this logic gives me error
Uncaught TypeError: Cannot read property 'isMale' of null
What will be the best login to handle this condition ?
Thanks !
I have an object that I need to check if it is defined. Also, I also want to check if a property of that object is true or false.
So what I want is
if ((typeof myVar !== 'undefined') && (myVar.isMale === false)) {
// Do something 1
}
else{
// Do something 2
}
But this logic gives me error
Uncaught TypeError: Cannot read property 'isMale' of null
What will be the best login to handle this condition ?
Thanks !
Share Improve this question edited Aug 18, 2014 at 6:57 user2864740 62.1k15 gold badges158 silver badges227 bronze badges asked Aug 18, 2014 at 6:43 Ateev ChopraAteev Chopra 1,0263 gold badges16 silver badges32 bronze badges 4- 2 'undefined' and 'null' are two different things. you are checking if the object is 'undefined' not when it is null – Prabhu Murthy Commented Aug 18, 2014 at 6:44
- first check if myVar is undefined, then, once you know it is not undefined, check for its properties. In this way, if typeof myVar is undefined it can't have any property, therefore your if will always fail. – briosheje Commented Aug 18, 2014 at 6:44
- after checking 'undefined' you should check for null, if ( typedef myVar != 'undefined' && myVar !== null && myVar.isMale === false) { // do something} – Sadegh Commented Aug 18, 2014 at 6:47
-
Ofcourse I thought of it. But Can you express this in
if
else
conditions. I need to have both conditions toDo Something 1
. Else it should go toDo Something 2
– Ateev Chopra Commented Aug 18, 2014 at 6:48
2 Answers
Reset to default 6You need to test further, either by exclusion:
if (typeof myVar != 'undefined' && myVar && myVar.isMale === false) {
or by inclusion:
if (typeof myVar == 'object' && mVar && myVar.isMale === false) {
but there are objects that return values other than "object" with typeof tests (e.g. host objects may and Function objects do).
or by explicit conversion:
if (typeof myVar != 'undefined' && Object(myVar).isMale === false) {
Edit
The additional && myVar
test is to catch NaN and null which pass the typeof test.
using optional chaining operator (?.) to access the property isMale
of the object myVar
. optional chaining allows safe access to the properties of an object, even if the object itself might be null
or undefined
rather than causing an error.
if (myVar?.isMale) {
// Do something 2
} else {
// Do something 1
}
本文标签: javascriptquotCannot read propertyof nullquot after checking for undefined objectStack Overflow
版权声明:本文标题:javascript - "Cannot read property .. of null" after checking for undefined object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744058044a2583613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论