javascript中的!==是什么?

What is !== in javascript?

本文关键字:是什么 中的 javascript      更新时间:2023-09-26

代码用于验证图像或单元格选择。我的问题是

在下列函数中使用!==做什么:
function checkSelected() {
    var cellList,
        selectedCell,
        imgList,
        selectedImg,
        i
    cellList = document.getElementById("imgTable")
    cellList = cellList.getElementsByTagName("td")
    imgList = document.getElementById("b_list")
    imgList = imgList.getElementsByTagName("img")
    if (cellList[0].style.border.indexOf("7px outset") !== -1) { selectedCell = cellList[0] }

    if (selectedCell === undefined || selectedImg === undefined) { return }
    selectedCell.style.backgroundImage = "url(" + selectedImg.src + ")"
    selectedCell.firstChild.style.visibility = "hidden"
    selectedCell.style.border = "1px solid"
    selectedImg.style.border = "1px solid"
}

!==是一个更严格的不等式,它不会对操作数执行任何类型强制转换,而!=会执行类型强制转换。

因此,如果操作数不相等且/或类型不同,!==将返回true。

相反,如果操作数相等,!=返回true。如果操作数不是同一类型,JavaScript将尝试将两个操作数转换为适合比较的形式。如果操作数是对象,那么JavaScript将比较它们的引用(内存地址)。最好的示例如下:

"3" != 3; // returns false because JavaScript will perform 
          // type coercion and compare the values
"3" !== 3; // returns true because the first operand is a
           // string whereas the second is an integer. Since
           // they are of different types, they are not equal.

有关更多信息,请查看MDN上的比较运算符

它的意思是"不严格等于",而!=的意思是"不等于"。

检查相等性的方法有两种:=====,它们分别是"相等"answers"严格相等"。要查看确切的差异,请查看此表。

!=!==只是这些操作的对应的负运算。例如a !== b!(a === b)是一样的