Javascript 如何测试是否定义了response.location.id?

Javascript 如何测试是否定义了response.location.id?,javascript,variables,testing,attributes,defined,Javascript,Variables,Testing,Attributes,Defined,如何测试是否: response.location.id 定义是什么 多谢各位 if (response && response.location && typeof response.location.id !== "undefined"){ // is defined } 如果您不能依赖定义的其余部分,那么: response && response.location && typeof response.locati

如何测试是否:

response.location.id
定义是什么

多谢各位

if (response && response.location && typeof response.location.id !== "undefined"){

 // is defined

}
如果您不能依赖定义的其余部分,那么:

response && response.location && typeof response.location.id !== "undefined"

如果您可以依赖它们,并且不必担心
null
false
0
作为定义的
id
的值,那么直接测试它(
If(response.location.id)
)。

您尝试过什么吗?也许:
if(response.location.id){//it's defined}或者{//it's not}
虽然几乎不可取,但是可以执行
var undefined
,这使得
typeof
的使用更加健壮。不过,无可否认,在面对一些相当反常的事情时,它非常健壮。
typeof response.location.id!==“未定义”
当然可以?@naveen同意。javascript中没有
未定义的
关键字,因此这取决于没有定义它。它相当于
!==那么什么是ChancessomeOtherCode定义他的名字呢?
也不会
响应。location.id
如果未定义就可能是错误的?虽然如果我们知道这不是有效的id值,那么@naveen会有一个更简洁的方法,因此我建议它是否可以使用。
response && response.location && typeof response.location.id !== "undefined"