JavaScript 中的 !== 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16176062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:35:10  来源:igfitidea点击:

What is !== in javascript?

javascript

提问by Cr4sh Over

The code is used to verify an image or cell selection. My question is What is !== used for in the following function:

该代码用于验证图像或单元格选择。我的问题是什么是 !== 用于以下函数:

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"
}

回答by Vivin Paliath

!==is a stricter inequality that does not perform any type coercion on the operands, compared to !=, which does perform type coercion.

!==是一个更严格的不等式,它不对操作数执行任何类型强制,相比之下!=,它确实执行类型强制。

So !==will return true if the operands are not equal and/or not of the same type.

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

In contrast !=returns true if the operands are equal. If the operands are not of the same type, JavaScript will try to convert both operands into a form suitable for comparison. If the operands are objects, then JavaScript will compare their references (memory addresses). This is best demonstrated as follows:

相反,!=如果操作数相等则返回真。如果操作数的类型不同,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.

For more information, take a look at Comparison Operatorson MDN.

欲了解更多信息,看看比较操作上MDN。

回答by Cam

It means "not strictly equal to", as opposed to !=which means "not equal to".

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

There are two ways to check for equality: ==and ===, which are "equals" and "strictly equals" respectively. To see the exact difference, check out this table.

有两种检查相等性的方法:==and ===,分别是“相等”和“严格相等”。要查看确切差异,请查看此表

!=and !==are simply the corresponding negations of those operations. So for example a !== bis the same as !(a === b)

!=并且!==只是这些操作的相应否定。所以例如a !== b!(a === b)