-5

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"
}
Cr4sh Over
  • 69
  • 1
  • 1
  • 1

2 Answers2

9

!== 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.

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:

"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 Operators on MDN.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
5

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 !== are simply the corresponding negations of those operations. So for example a !== b is the same as !(a === b)

Cam
  • 14,930
  • 16
  • 77
  • 128
  • I see, but, what is it used for in the IF statement? is it used to verify Null? – Cr4sh Over Apr 23 '13 at 18:08
  • The only place I see it in the code you shared is `.indexOf("7px outset") !== -1`. This means "If value of `.indexOf` is not `-1` (invoked with that param), give `true`. We know `.indexOf` on a string will have type _Number_, therefore we can happily use `!== -1`, as `-1` is also a _Number_. – Paul S. Apr 23 '13 at 18:29
  • To build on what Paul S said, it may be of interest to you that `-1 == "-1"` is **true** whereas `-1 === "-1"` is **false**. – Cam Apr 23 '13 at 20:08