When would JavaScript == make more sense than ===?

后端 未结 4 2025
太阳男子
太阳男子 2021-01-01 17:22

As Which equals operator (== vs ===) should be used in JavaScript comparisons? indicates they are basically identical except \'===\' also ensures type equality

4条回答
  •  忘掉有多难
    2021-01-01 18:08

    Consider a situation when you compare numbers or strings:

    if (4 === 4)
    {
      // true
    }
    

    but

    if (4 == "4")
    {
      // true
    }
    

    and

    if (4 === "4")
    {
      // false
    }
    

    This applies to objects as well as arrays.

    So in above cases, you have to make sensible choice whether to use == or ===

提交回复
热议问题