javascript中的平等

equality in javascript

本文关键字:javascript      更新时间:2023-09-26

在javascript中工作时,有人能为我提供一个关于平等/不平等和类型强制测试的良好参考或解释吗?

从我所读到的内容中,我发现使用eqeq(==)和eqeqeq的思想有两个原则——有些人认为你不应该使用eqeq,总是使用eqeqeq因为它更安全。

我一直在玩一些基本的样品,我很难辨别区别,或者什么时候最好使用一个:

例如:这是我写的一些基本脚本。当我使用eqeq或eqeqeq进行测试时,我得到了相同的结果。我还没有看到一个例子,我会得到不同的结果(即使用eqeq返回true,而eqeqeq则返回false)。

function write(message){
  document.getElementById('message').innerHTML += message +'<br/>';
}

var tim = { name: "tim" };
var tim2 = { name: "tim" };
//objects are equal to themselves ( == vs ==== eqeq or eqeqeq)
write("tim eq tim: " + (tim == tim)); //returns true
//objects are only equal to themselves regardless of containing value got that
write("tim eq tim2: " + (tim === tim2)); //returns false
//access the primative type to test true or false
write("tim value eq tim2 value: " + (tim.name === tim2.name)); //returns true
//how does this differ in efficency over the eqeq operator? is one safer to use over the other?
//write("tim value eq tim2 value: " + (tim.name == tim2.name)); //also returns true
//testing primatives
write("apple eqeqeq apple: " + ("apple" === "apple"));  //true
write("apple eqeqeq apple: " + ("apple" == "apple"));  //true

有人能提供一个我能读到的解释或参考吗?这有助于进一步澄清这一点。

欢呼,

===和===之间的区别很简单:===是值的比较。===是值和类型的比较。使用===将阻止JavaScript动态确定类型,并按原样比较值。

5 == "5"     //true - JS compares the number 5 to a string of "5" and determines the contents are the same 
5 === "5"    //false - A character is not a number, they can't be the same.
0 == false   //true - false is a bool, 0 is numerical, but JS determines that 0 evaluates to false
0 === false  //false - numeric is not boolean, they can't be exactly the same thing
5 == 5.0     //true - need I say more?
5 === 5.0    //true - one is a float and one is an int, but JS treats them all as numerical

我发现,对于函数可以同时返回false(表示失败)和0(作为合法结果)的测试来说,这一点至关重要。

JS共有5种基元类型:numerical、string、boolean、null和undefined要求两个参数都具有相同的类型相等的值才能返回true。没有float、int、long、short等-任何类型的数字都被归为数字。

这很简单。
==执行类型转换,然后将转换后的值与所需值进行比较
===不执行类型转换,而是直接比较您的值
显然===在性能和准确性方面更好,但在某些情况下===也很方便,所以如果它们适合您的需求,您可以同时使用它们。

比较

干杯!

下面是一个非常完整的列表,列出了在使用相等运算符时您不会想到的事情

以下所有情况均为真

0               ==          ""
0               ==          " "
0               ==          []
false           ==          ""
false           ==          " "
false           ==          []
[]              ==          ""
null            ==          undefined
"str"           ==          ["str"]
"1"             ==          true
"0"             ==          false
"null"          !=          null
"false"         !=          false
"true"          !=          true
"undefined"     !=          undefined
"NaN"           !=          NaN
NaN             !=          NaN         //exception: NO exception
{}              !=          {}          //exception: same reference
[]              !=          []          //exception: same reference
--------------------------------------
new Number(10)      !==         10
new String("str")   !==         "str"
NaN                 !==         NaN     //exception: NO exception
{}                  !==         {}      //exception: same reference
[]                  !==         []      //exception: same reference

(其中一些来自此来源)

总之,永远不要使用==。即使你想:

"强烈建议只使用严格相等运算符在需要强制类型的情况下,应该显式执行,并且不受语言复杂的强制规则的影响。"

(来源)

===是一个严格的等式运算符,只有当变量具有相同的类型和值时,它才会返回true。

a = 1;
b = "1";

a == b将返回true,a === b将返回false

==的规则不容易记住,你能得到下面例子的所有正确答案吗?

"" == "0" // false
0 == "" // true
0 == "0" // true
false == "false" // false
false == "0" // true
false == undefined // false
false == null // false
null == undefined // true
" 't'r'n" == 0 // true

所以最好只使用===,而不要使用==

您一定听说过javascript是一种非常松散类型的语言。

正如上面超现实主义所说当您只想比较参数的值而不关心它们的类型时,会使用(==)运算符如5=="5"将返回true。当你想看到(例如,用户按下了什么键,而你不在乎它的类型可能是字符串、字符或整数)时,就会出现这种情况。

但在某些情况下,争论的类型也很重要。在这种情况下,您需要比较运算符的类型。因此,在这些情况下,使用三重等式运算符。例如,如果正在执行一些加法或乘法运算,则需要确保操作数的类型兼容。所以你使用三重(===)运算符。