Javascript和未定义的值

Javascript and undefined values

本文关键字:未定义 Javascript      更新时间:2023-09-26

我想检查对象中的值是否未定义:我有以下代码

for (i=0; i<extra.points.length; i++){
    console.log(extra.points[i].coord)
    if (!extra.points[i].coord === undefined){
        console.log("creating");
        //rest of code
    }
}

extra具有以下值:

{
    points:[
       {name:'adp', coord:undefined}, 
       {name:'pdp', coord:{x:324, y:482}}
    ], 
    bicetrix:[]
}

但它根本不会进入if。我做错了什么?

运算符的顺序很重要。在===之前未评估!

我猜你是指

if (extra.points[i].coord !== undefined){
        console.log("creating");
        //rest of code
}

你所写的意思是"如果不是(某事)严格等于未定义"。不是什么东西总是布尔的,即真或假。无论如何都不可能等于未定义。

!undefinedtrue,而true === undefined始终是false