在JavaScript比较中应该使用哪个等于运算符(==vs===)?
我正在使用JavaScript,它返回了许多建议,在执行比较在JavaScript比较中应该使用哪个等于运算符(==vs===)?,javascript,operators,equality,equality-operator,identity-operator,Javascript,Operators,Equality,Equality Operator,Identity Operator,我正在使用JavaScript,它返回了许多建议,在执行比较idSele_UNVEHtype.value.length==0if语句时,将====(两个等号)替换为=(三个等号) 用===替换==是否有性能优势 由于存在许多比较运营商,任何性能改进都将受到欢迎 如果没有发生类型转换,除了没有进行类型转换外,严格相等运算符(==)的行为与抽象相等运算符(==)的行为是相同的,类型必须相同才能视为相等 参考: 执行任何必要的类型转换后,=运算符将进行相等性比较。==运算符将不执行转换,因此如果两个值
idSele_UNVEHtype.value.length==0
if语句时,将====
(两个等号)替换为=
(三个等号)
用===
替换==
是否有性能优势
由于存在许多比较运营商,任何性能改进都将受到欢迎
如果没有发生类型转换,除了没有进行类型转换外,严格相等运算符(
==
)的行为与抽象相等运算符(==
)的行为是相同的,类型必须相同才能视为相等
参考:
执行任何必要的类型转换后,=
运算符将进行相等性比较。==
运算符将不执行转换,因此如果两个值的类型不同,==
将简单地返回false
。两者都同样快
引用道格拉斯·克罗克福德(Douglas Crockford)的优秀作品
JavaScript有两组相等运算符:==
和==还有他们的邪恶双胞胎=
和=代码>。好的按你所期望的方式工作。如果两个操作数类型相同且值相同,则==
生成true
和==代码>产生<代码>错误
。当操作数的类型相同时,邪恶的孪生兄弟会做正确的事情,但如果它们的类型不同,他们会试图强制值。他们这样做的规则既复杂又难以理解。以下是一些有趣的案例:
'' == '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
缺乏及物性令人担忧。我的建议是永远不要使用邪恶的双胞胎。相反,请始终使用==
和==代码>。刚才显示的所有比较都使用=
运算符生成false
更新:
在评论和关于对象的问题上,他提出了一个很好的观点。对于对象,==
和==
的行为彼此一致(特殊情况除外)
特殊情况是,由于其toString
或valueOf
方法,将一个基本体与计算结果为同一基本体的对象进行比较。例如,考虑将字符串原语与使用<代码>字符串< /COD>构造函数创建的字符串对象进行比较。
"abc" == new String("abc") // true
"abc" === new String("abc") // false
这里,=
操作符检查两个对象的值并返回true
,但是==
看到它们不是相同的类型并返回false
。哪一个是正确的?这取决于你想比较什么。我的建议是完全绕过这个问题,只是不要使用String
构造函数从字符串文本创建字符串对象
参考
使用=
运算符(相等)
使用==
运算符(标识)
这是因为相等运算符==
执行类型强制,这意味着解释器在比较之前隐式地尝试转换值
另一方面,标识运算符==
不执行类型强制,因此在比较时不转换值,因此跳过一个步骤时速度更快(根据测试)。在您的使用中,这两个操作之间不太可能有任何性能差异。由于两个参数已经是相同的类型,因此不需要进行类型转换。两个操作都将进行类型比较,然后进行值比较。将==运算符称为严格比较运算符,它与=运算符不同
让我们取两个变量a和b
对于“a==b”要计算为真,a和b需要是相同的值
在的情况下,“a===b”a和b必须是相同的值,也是相同的类型,才能计算为真
以下面的例子为例
var a = 1;
var b = "1";
if (a == b) //evaluates to true as a and b are both 1
{
alert("a == b");
}
if (a === b) //evaluates to false as a is not the same type as b
{
alert("a === b");
}
总之;在您不希望的情况下,使用==运算符可能会计算为true,因此使用==运算符会更安全
在90%使用率的情况下,使用哪一个并不重要,但当您有一天出现一些意外行为时,您可以很方便地了解差异。在典型的脚本中,性能不会有任何差异。更重要的可能是,千“==”比千“==”:)重1KB这一事实可以告诉您在您的情况下是否存在性能差异
但就我个人而言,我会按照JSLint的建议去做。之所以有此建议,不是因为性能问题,而是因为类型强制意味着('\t\r\n'==0)
是正确的。我在Firefox中使用以下代码对此进行了测试:
function isEqual(x, y) { // if `==` were a function
if(typeof y === typeof x) return y === x;
// treat null and undefined the same
var xIsNothing = (y === undefined) || (y === null);
var yIsNothing = (x === undefined) || (x === null);
if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);
if(typeof y === "function" || typeof x === "function") {
// if either value is a string
// convert the function into a string and compare
if(typeof x === "string") {
return x === y.toString();
} else if(typeof y === "string") {
return x.toString() === y;
}
return false;
}
if(typeof x === "object") x = toPrimitive(x);
if(typeof y === "object") y = toPrimitive(y);
if(typeof y === typeof x) return y === x;
// convert x and y into numbers if they are not already use the "+" trick
if(typeof x !== "number") x = +x;
if(typeof y !== "number") y = +y;
// actually the real `==` is even more complicated than this, especially in ES6
return x === y;
}
function toPrimitive(obj) {
var value = obj.valueOf();
if(obj !== value) return value;
return obj.toString();
}
console.time(“测试质量”);
var n=0;
while(true){
n++;
如果(n==100000)
打破
}
控制台。时间结束(“测试质量”)代码>问题是您可能很容易陷入麻烦,因为JavaScript有很多隐式转换,这意味着
var x = 0;
var isTrue = x == null;
var isFalse = x === null;
这很快就成了一个问题。隐式转换为什么是“邪恶”的最好的例子可以从这个代码中获得,实际上,C++将被编译,因为从CString隐式转换为指针TyPulf类型……/P>
CString x;
delete x;
很明显,在运行时会做一些未定义的事情
谷歌在C++中隐式转换,并得到一些反对它的参数…
在这里的答案中,我没有读到关于<强>相等< /强>意味着什么。有人会说===
var x = 0;
var isTrue = x == null;
var isFalse = x === null;
CString x;
delete x;
var a = [1,2,3];
var b = [1,2,3];
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
var a = { x: 1, y: 2 };
var b = { x: 1, y: 2 };
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
var a = { };
var b = { };
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
var a = "12" + "3";
var b = "123";
alert(a === b); // returns true, because strings behave like value types
var a = new String("123");
var b = "123";
alert(a === b); // returns false !! (but they are equal and of the same type)
'1' === 1 // will return "false" because `string` is not a `number`
0 == '' // will be "true", but it's very common to want this check to be "false"
null == undefined // returns "true", but in most cases a distinction is necessary
4 == "4" // will return true
4 === "4" // will return false
0==false // true,although they are different types
0===false // false,as they are different types
2=='2' //true,different types,one is string and another is integer but
javaScript convert 2 to string by using == operator
2==='2' //false because by using === operator ,javaScript do not convert
integer to string
2===2 //true because both have same value and same types
$a = 0;
$a==0;
$a==NULL;
$a==false;
$a = 0;
$a===0; // returns true
$a===NULL; // returns false
$a===false; // returns false
'' == 0 == false // Any two values among these 3 ones are equal with the == operator
'0' == 0 == false // Also a set of 3 equal values, note that only 0 and false are repeated
'\t' == 0 == false // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'\r' == 0 == false // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'\n' == 0 == false // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'\t\r\n' == 0 == false // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
null == undefined // These two "default" values are not-equal to any of the listed values above
NaN // NaN is not equal to any thing, even to itself.
if( value == null ){
// value is either null or undefined
}
// Check for both undefined and null values, for some important reason.
undefOrNull == null;
0==false // true
0===false // false, because they are of a different type
1=="1" // true, auto type coercion
1==="1" // false, because they are of a different type
var a = [1, 2, 3];
var b = [1, 2, 3];
console.log(a == b) // false
console.log(a === b) // false
>>> 1 == 1
true
>>> 1 == 2
false
>>> 1 == '1'
true
>>> 1 === '1'
false
>>> 1 === 1
true
var a;
var b = null;
<script>
function onPageLoad()
{
var x = "5";
var y = 5;
alert(x === 5);
};
</script>
</head>
<body onload='onPageLoad();'>
2 == '2' -> true, values are SAME because of type conversion.
2 === '2' -> false, values are NOT SAME because of no type conversion.
'0' == false // true
[1] == true // true
[] == false // true
[[]] == false // true
[0] == false // true
[1,2,3] == '1,2,3' // true - REALLY?!
'\r\n\t' == 0 // true - Come on!
let A = '' // empty string
let B = 0 // zero
let C = '0' // zero string
A == B // true - ok...
B == C // true - so far so good...
A == C // **FALSE** - Plot twist!
(A == B) && (B == C) // true
(A == C) // **FALSE**
function isEqual(x, y) { // if `==` were a function
if(typeof y === typeof x) return y === x;
// treat null and undefined the same
var xIsNothing = (y === undefined) || (y === null);
var yIsNothing = (x === undefined) || (x === null);
if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);
if(typeof y === "function" || typeof x === "function") {
// if either value is a string
// convert the function into a string and compare
if(typeof x === "string") {
return x === y.toString();
} else if(typeof y === "string") {
return x.toString() === y;
}
return false;
}
if(typeof x === "object") x = toPrimitive(x);
if(typeof y === "object") y = toPrimitive(y);
if(typeof y === typeof x) return y === x;
// convert x and y into numbers if they are not already use the "+" trick
if(typeof x !== "number") x = +x;
if(typeof y !== "number") y = +y;
// actually the real `==` is even more complicated than this, especially in ES6
return x === y;
}
function toPrimitive(obj) {
var value = obj.valueOf();
if(obj !== value) return value;
return obj.toString();
}